我正在使用 Instruments 运行一个非常基本的测试应用程序,它发现了很多内存泄漏!因为我知道 Apple 人员会在将 App 提交到 iTunes 时执行泄漏内存检查,所以我想调查这个问题。
我的环境:Mac OS X 10.6.6 上的 MonoDevelop 2.4.2 和 MonoTouch 3.2.4,目标是运行 iOS 4.2.1 的 iPad。
我的测试应用仅显示一个 TableView,其中填充了 50 个字符串的列表,并按它们的首字母对它们进行分组。
重现问题的步骤:用 MonoDevelop 创建一个新的“基于 iPad 窗口的项目”,用 Interface Builder 打开 MainWindow.xib 文件,在窗口上放置一个新的 TableView 并创建它的导出(名为“tview”)以AppDelegate 类。 然后在Main.cs中输入如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace SimpleTable
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
public partial class AppDelegate : UIApplicationDelegate
{
private List<string> _names;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
_names = new List<string> { "Smith", "Jones", "Williams", "Brown", "Taylor",
"Davies", "Wilson", "Evans", "Thomas", "Johnson",
"Roberts", "Walker", "Wright", "Robinson", "Thompson",
"White", "Hughes", "Edwards", "Green", "Hall",
"Wood", "Harris", "Lewis", "Martin", "Jackson",
"Clarke", "Clark", "Turner", "Hill", "Scott",
"Cooper", "Morris", "Ward", "Moore", "King",
"Watson", "Baker", "Harrison", "Morgan", "Patel",
"Young", "Allen", "Mitchell", "James", "Anderson",
"Phillips", "Lee", "Bell", "Parker", "Davis" };
tview.Source = new MyTableViewSource(_names);
window.MakeKeyAndVisible ();
return true;
}
private class MyTableViewSource : UITableViewSource
{
private List<string> _sectionTitles;
private SortedDictionary<int, List<string>> _sectionElements = new SortedDictionary<int, List<string>>();
public MyTableViewSource(List<string> list)
{
// Use LINQ to find the distinct set of alphabet characters required.
_sectionTitles = (from c in list select c.Substring(0, 1)).Distinct().ToList();
// Sort the list alphabetically.
_sectionTitles.Sort();
// Add each element to the List<string> according to the letter it starts with
// in the SortedDictionary<int, List<string>>.
foreach (string element in list)
{
int sectionNum = _sectionTitles.IndexOf(element.Substring(0, 1));
if (_sectionElements.ContainsKey(sectionNum))
{
// SortedDictionary already contains a List<string> for that letter.
_sectionElements[sectionNum].Add(element);
}
else
{
// First time that letter has appeared, create new List<string> in the SortedDictionary.
_sectionElements.Add(sectionNum, new List<string> { element });
}
}
}
public override int NumberOfSections(UITableView tableView)
{
return _sectionTitles.Count;
}
public override string TitleForHeader(UITableView tableView, int section)
{
return _sectionTitles[section];
}
public override int RowsInSection(UITableView tableview, int section)
{
return _sectionElements[section].Count;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
string kCellIdentifier = "mycell";
UITableViewCell cell = tableView.DequeueReusableCell(kCellIdentifier);
if (cell == null)
{
// No re-usable cell found, create a new one.
cell = new UITableViewCell(UITableViewCellStyle.Default, kCellIdentifier);
}
string display = _sectionElements[indexPath.Section][indexPath.Row];
cell.TextLabel.Text = display;
return cell;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
string display = _sectionElements[indexPath.Section][indexPath.Row];
showAlert("RowSelected", "You selected: \"" + display + "\"");
// Prevent the blue 'selection indicator' remaining.
tableView.DeselectRow(indexPath, true);
}
private void showAlert(string title, string message)
{
using (var alert = new UIAlertView(title, message, null, "OK", null))
{
alert.Show();
}
}
}
}
}
我在设备上执行了以下测试:
注释掉
public override string TitleForHeader(UITableView tableView, int section)
程序,从 Instruments 中启动应用程序:发现单个泄漏;似乎这种泄漏总是存在,即使在测试一个空应用程序时也是如此!
未评论
public override string TitleForHeader(UITableView tableView, int section)
程序,从 Instruments 中启动应用程序:发现很多泄漏,并且当上下滚动表格和/或选择任何行时,它们的数量会增加。
替换
return _sectionTitles[section];
声明在
public override string TitleForHeader(UITableView tableView, int section)
过程
return "Test Header…";
(因此使用常量字符串):与测试 n.2 相同!
MonoTouch 是否有问题或我忘记了重要的事情?如果即使是这样一个简单的应用程序在运行几分钟后也会产生数百次内存泄漏,那么真实(且更复杂)的应用程序会发生什么情况?
我在网上进行了广泛的搜索,但没有找到任何关于此问题的重要帖子...任何贡献都将不胜感激。
最佳答案
monotouch 和 mono vm 分配和管理内存的方式并没有被仪器完全理解。 Apple 并未拒绝任何对此的申请。如果您有一个您认为是泄漏的特定错误,请在 http://monotouch.net/Support 的 monotouch 错误跟踪器中提交问题。
话虽如此,我查看了您的屏幕截图并确实发现了 1 个可能发生的 16 字节泄漏,以及您第二个屏幕截图中的泄漏,并为即将推出的 monotuch 4 修复了该泄漏。
关于memory-leaks - 为什么即使是非常简单的应用程序,MonoTouch 也会导致大量内存泄漏(如 Instruments 所报告的那样)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5003419/