我正在使用下面的代码创建一个基于UI元素的实时图块.它渲染uiElementa WriteableBitmap,保存位图+返回文件名.此方法在Windows Phone后台任务代理中运行,我正在运行内存限制.
private string CreateLiveTileImage(Canvas uiElement, int width, int heigth)
{
var wbmp = new WriteableBitmap(width, heigth);
try
{
wbmp.Render(uiElement, null);
wbmp.Invalidate();
var tileImageName = _liveTileStoreLocation;
using (var stream = new IsolatedStorageFileStream(tileImageName, FileMode.Create, FileAccess.Write, IsolatedStorageFile.GetUserStoreForApplication()))
{
wbmp.SaveJpeg(stream, width, heigth, 0, 100);
stream.Close();
}
uiElement = null;
wbmp = null;
GC.Collect();
return "isostore:" + tileImageName;
}
catch (Exception exception)
{
// ...
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我做了一些测试,问题是:这个方法泄漏内存,但我不知道为什么/在哪里?!
我也做了一些测试运行 - 在第一次进入这个方法之前:
Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage
7.249.920 Bytes
Run Code Online (Sandbox Code Playgroud)
这是好的,因为附加了调试器,它使用大约2 MB的内存.
再做一些此方法的运行(在调试器中再次设置为run方法):
Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage …Run Code Online (Sandbox Code Playgroud)