【发布时间】:2014-01-16 23:11:21
【问题描述】:
如何减少我的 C# Windows Phone 应用程序的内存使用量?
一些实例:
1)。使用方法:LockScreen.GetImageUri()
我可以在 cs 文件的顶部添加using Windows.Phone.System.UserProfile;。
或者在它前面加上前缀Windows.Phone.System.UserProfile.,就是Windows.Phone.System.UserProfile.LockScreen.GetImageUri()
哪个会使用更少的内存?
2)。考虑变量的范围,如果我将方法分成多个部分并逐个运行它们,它会更频繁地释放内存吗?
例如我需要使用WriteableBitmap 渲染一些图像,每个可能会消耗 1MB 内存,如果我有 10 个或更多图像要渲染,它可能很快就会超过内存限制。
如果我用不同的方法渲染它们会有帮助吗?
3)。哪个是更好的选择:静态还是非静态?
似乎只要应用程序“活着”或“运行”,静态对象就会一直存在于内存中,但是,要使用非静态方法,我们需要创建它的一个实例,每次都会消耗内存我们这样做(不是吗?)。
添加:如果我创建了一个类对象的实例,我仍然可以“释放”它吗?
一种特殊情况:使用IsolatedStorageSettings.ApplicationSettings;
我可以像这样使用它:
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("IconSet"))
{
settings["IconSet"] = "Set1";
}
或者我也可以使用
if (!IsolatedStorageSettings.ApplicationSettings.Contains("IconSet"))
{
IsolatedStorageSettings.ApplicationSettings["IconSet"] = "Set1";
}
有什么不同吗? (关于内存使用)
4)。 Deployment.Current.Dispatcher.BeginInvoke(() =>{})
这个方法会释放它使用的内存吗?
还是我需要任何特殊的方法来手动释放内存?比如EndInvoke()?
【问题讨论】:
标签: c# memory-management windows-phone-8