【发布时间】:2013-03-21 00:38:55
【问题描述】:
大家好,我的 WP8 应用程序的 ScheduledAgent 出现内存泄漏问题。我正在尝试做的是循环更新应用程序的多个图块,看起来还可以(就内存使用而言),但由于某种原因,更新图块后内存没有释放。
我的代码如下所示(Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage 测量的内存使用情况):
protected override void OnInvoke(ScheduledTask task)
{
try
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// 5MB used
foreach (int id in myIdsList)
UpdateTile(id);
});
}
catch (Exception e)
{
if (Debugger.IsAttached)
Debugger.Break();
}
NotifyComplete();
}
更新方法如下:
public void UpdateTile(int id)
{
MyClass myClassInstance = GetInstanceById(id);
//~6MB used by now
Canvas drawingSurface = new Canvas();
//Add some Image objects to canvas (source to each image is a filePath
// contained in myClassInstance)
//~7MB
WriteableBitmap bigTileImage = new WriteableBitmap(691, 336);
bigTileImage.Render(drawingSurface, null);
bigTileImage.Invalidate();
//~9MB
var bigTilePath = string.Format(/*path here*/);
using (IsolatedStorageFile storage =
IsolatedStorageFile.GetUserStoreForApplication())
{
if (storage.FileExists(bigTilePath))
storage.DeleteFile(bigTilePath);
using (var isoFileStream = new IsolatedStorageFileStream(
bigTilePath, FileMode.Create, storage))
{
bigTileImage.SaveJpeg(isoFileStream, bigTileImage.PixelWidth,
bigTileImage.PixelHeight, 0, 100);
}
}
ShellTile tileToUpdate = ShellTile.ActiveTiles.FirstOrDefault(
x => x.NavigationUri.ToString().Contains("TileID="+id));
FlipTileData flipTileData = new FlipTileData()
{
//Set fields
WideBackgroundImage =
new Uri(("isostore:/"+bigTilePath, UriKind.Absolute),
};
tileToUpdate.Update(flipTileData);
//~10MB used
//Shouldn't memory be released by now??
// calling GC.Collect() has no effect
}
所以,它是一个 PeriodicTask,我有 11MB 的内存上限,并且只要内存在迭代后没有释放,我就会得到 OutOfMemoryException。
也许我不了解基础知识,但我认为大部分占用的内存应该在将文件保存到 IS 后释放(或者每当 GC 决定收集时,但是,正如我所说,即使明确调用 GC.Collect() 也没有效果) ?
我做错了吗?有没有办法释放内存?
【问题讨论】:
-
尝试使用像ANTS Memory Profiler 这样的内存分析器,看看你的假设是对还是错。
-
你有没有试过在方法执行后调用GC.Collect()?也就是说,在 OnInvoke 中,在 UpdateTile() 之后调用 GC.Collect()。在方法结束时调用收集器不会生效,因为您仍然保留对所有对象的引用。
-
你永远不会处理
bigTileImage。 -
@gjulianm,是的,我试过了 - 没有效果,记忆仍然没有释放
-
@leppie bigTileImage 是可写位图,不能隐式转换为'System.IDisposable';
标签: c# windows-phone-7 memory-leaks windows-phone-8 scheduled-tasks