【发布时间】:2014-03-27 13:05:23
【问题描述】:
我使用 Xamarin 有一段时间了,遇到了一个很奇怪的问题。
我可以让一个非常简单的两屏应用程序崩溃。在第一个屏幕上,我有一个 UIButton 和 TouchUpInside 事件。第二个,我有一个带有附加图像的UIImageView(来自本地文件)。
我所要做的就是一直在这两个视图控制器之间来回移动。
当我连接 XCode 的 Instruments 和 Activity Monitor 时,我注意到我的简单应用程序在内存被回收之前达到了 ~100MB 的内存,然后它的使用量下降到 ~15MB。
但是当我循环导航足够长的时间时,内存会达到 140MB 以上,并且应用程序会崩溃。我在开发更复杂的应用程序时发现了这种行为。当然,我正在采取所有可用的预防措施:
- 取消订阅 ViewWillDisappear 上的事件
- 将代表归零等。
基本上,在我的复杂应用程序中,我已经为所有UIViewControllers 覆盖了我的基类中的Dispose 方法,我可以看到Dispose 方法在每个视图控制器上都使用disposing == false 调用显示。但是,内存使用量并没有下降。
这有什么问题?
我想指出几点:
- 我的 Xamarin Studio 是最新的,
- 当我在 iPhone 3GS iOS 6.1.3 上以调试模式测试应用程序时出现崩溃
- 在简单的应用程序中,图像是 1024x1024 JPG 文件。
这里有一些代码示例:
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);
}
}
【问题讨论】:
标签: c# memory xamarin memory-management xamarin.ios