我已经使用了一段时间,我遇到了一个非常奇怪的问题。
我可以崩溃一个非常简单的两个屏幕应用程序。在第一个屏幕上,我有一个UIButton和TouchUpInside事件。在第二部分,我有一个带有附加图像的UIImageView (来自本地文件)。
我所要做的就是在这两个视图控制器之间来回移动,一直这样。
当我将XCode的仪器与Activity连接起来时,我注意到我的简单应用程序在内存被回收之前达到了大约100 my的内存,然后它的使用量下降到了~15 my。
但是,当我循环导航足够长时,内存超过140 app,应用程序就会崩溃。我在开发一个更复杂的应用程序时发现了这种行为。当然,我正在采取一切可用的预防措施:
最基本的是,在我的复杂应用程序中,我已经为所有的Dispose在基类中重写了UIViewControllers方法,并且我可以看到在显示的每个视图控制器上都用disposing == false调用了Dispose方法。然而,内存使用量并没有下降。
这是怎么回事?
我想指出以下几点:
随函附上一些代码示例:
public partial class SimpleTestViewController : UIViewController
{
private UIButton button;
public SimpleTestViewController () : base ("SimpleTestViewController", null) { }
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
button = new UIButton (new RectangleF(0, 100, 100, 50));
button.BackgroundColor = UIColor.Red;
button.TouchUpInside += (sender, e) => {
this.NavigationController.PushViewController(new SecondView(), true);
};
this.Add (button);
// Perform any additional setup after loading the view, typically from a nib.
}
}
public partial class SecondView : UIViewController
{
private UIImageView _imageView;
public SecondView () : base ("SecondView", null) { }
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
_imageView = new UIImageView (new RectangleF(0,0, 200, 200));
_imageView.Image = UIImage.FromFile ("Images/image.jpg");
this.Add (_imageView);
// Perform any additional setup after loading the view, typically from a nib.
}
protected override void Dispose (bool disposing)
{
System.Diagnostics.Debug.WriteLine("Disposing " +this.GetType()
+ " hash code " + this.GetHashCode()
+ " disposing flag "+disposing);
base.Dispose (disposing);
}
}发布于 2014-03-27 14:43:56
您正在创建按钮/图像的实例,并将其存储在后台字段中,而不是在控制器的Dispose中对其调用Dispose。Controller实例化了它们,所以您必须清除它们。
在上面的示例中,您还没有解除按钮TouchUpInside事件的连接。我建议不要为此使用lambda,并为此创建一个方法,以便以后更容易分离。
TouchUpInside -= this.Method; 此外,您不会将图像从添加到的视图中移除。
我知道你说你在视图中做这些事情,它会破坏,但是在你的示例代码中没有发生这种情况。你能提供一个充分发挥作用的例子,这些基本的照顾?
https://stackoverflow.com/questions/22688561
复制相似问题