在我的应用程序中,使用Xamarin Profiler,我注意到无论何时将VC推入堆栈并导航回内存分配都不是免费的。如果我再次推动相同的视图,它会增加更多的内存。向后导航不会释放内存
我创建了一个示例项目来测试,我发现这是做同样的事情。
示例项目:
我有两个视图控制器,VC1和VC2。 VC1是根视图控制器。
每当我从VC1推VC2时,都会分配内存,但是当我导航回时,内存不是免费的。如果我继续推VC2,它会增加更多内存。在VC2中,我通过设计师添加了3个标签。
中的AppDelegate:
namespace TestSample
{
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override UIWindow Window
{
get;
set;
}
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
Window = new UIWindow(UIScreen.MainScreen.Bounds);
var nav = new UINavigationController(new MyViewController());
Window.RootViewController = nav;
Window.MakeKeyAndVisible();
return true;
}
}
}
VC1:
namespace TestSample
{
public partial class MyViewController : UIViewController
{
public MyViewController() : base("MyViewController", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
btn1.TouchUpInside += Btn1_TouchUpInside;
}
void Btn1_TouchUpInside(object sender, EventArgs e)
{
NavigationController.PushViewController(new MyViewController2(), true);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
btn1.TouchUpInside -= Btn1_TouchUpInside;
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
}
VC2:在Xamarin.iOS
namespace TestSample
{
public partial class MyViewController2 : UIViewController
{
public MyViewController2() : base("MyViewController2", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
/*foreach (UIView view in View.Subviews) {
view.RemoveFromSuperview();
}*/
label1.RemoveFromSuperview();
label2.RemoveFromSuperview();
label3.RemoveFromSuperview();
label1.Dispose();
label2.Dispose();
label3.Dispose();
}
}
}
你可以发布你的VC2吗?这里有一个循环引用,需要通过设置null或调用Dispose()来切断关系,否则GC将永远看不到有资格收集的对象。 –
VC2只有3个标签,我通过设计者在XIB文件中添加了这些标签。编辑VC2 – PLOW
@PLOW如果强制使用'GC.Collect()',回收内存? – SushiHangover