出口与MEF在Caliburn.微增加存储器的问题
-
25-09-2019 - |
题
我有个问题与增加存储器。我用MEF在caliburn.微上建立新的画面-WPF窗口。
视图模型的视/视图这样的:
[Export(typeof(IChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IChatViewModel
{}
在设立我的使用ExportFactory,智能控制器是在这里:
public interface IViewModelsControler
{
ExportLifetimeContext<IChatViewModel> CreatChatViewModel();
}
[Export(typeof(IViewModelsControler))]
public class ViewModelsControler : IViewModelsControler
{
[Import]
public ExportFactory<IChatViewModel> ChatViewFactory { get; set; }
public ExportLifetimeContext<IChatViewModel> CreatChatViewModel()
{
return ChatViewFactory.CreateExport();
}
}
我用ViewModelsControler类ChatScreenManager类。这类开放/清除聊天的屏幕。
这里是它:
[Export(typeof(IChatScreenManager))]
public class ChatScreenManager : IChatScreenManager
{
private IWindowManager _windowManager;
[Import]
public IViewModelsControler ViewModelControler { get; set; }
[ImportingConstructor]
public ChatScreenManager(IWindowManager windowManager)
{
_windowManager = windowManager;
ActiveChatScreens = new Dictionary<string, ExportLifetimeContext<IChatViewModel>>();
}
//store active screen
public Dictionary<string, ExportLifetimeContext<IChatViewModel>> ActiveChatScreens { get; set; }
public void OpenChatScreen(DetailData oponent, string avatarNick, BitmapImage avatarImage)
{
if (!ActiveChatScreens.ContainsKey(oponent.Info.Nick))
{
//create new chat screen with view model controler
ExportLifetimeContext<IChatViewModel> chatScreen = ViewModelControler.CreatChatViewModel();
//show
_windowManager.Show(chatScreen.Value);
//add ref to the dic
ActiveChatScreens.Add(oponent.Info.Nick, chatScreen);
}
}
public void RemoveChatScreen(string clossingScreen)
{
MessageBox.Show(GC.GetTotalMemory(true).ToString());
ActiveChatScreens[clossingScreen].Dispose();
ActiveChatScreens.Remove(clossingScreen);
GC.Collect();
GC.SuppressFinalize(this);
MessageBox.Show(GC.GetTotalMemory(true).ToString());
}
}
和我的问题是:
- 我呼OpneChatScreen方法从ChatScreenManager它打开新的WPF窗口
- 添加参照上的这个窗口的字典。
- 当我闭窗口,我呼RemoveChatScreen.
在RemoveChaScreen:
- 我总记忆,例如它是K37,000
- 然后我打电话Dipose方法上ExportLifetimeContext chatScreen
- 力GC
- 并获得共用的存储器,例如它是39,000K
存储器的使用仍然增加。我希望,如果我呼叫处理方法上目ChatViewModel和也ChatView对象的这些对象的破坏。
解决方案
不力的GC!还有, Dispose()
方法应该按照清除从你的收藏。
public void RemoveChatScreen(string closingScreen)
{
MessageBox.Show(GC.GetTotalMemory(true).ToString());
IChatViewModel chatWindow = ActiveChatScreens[closingScreen]
// remove from collection - GC may pass over object referenced in collection
// until next pass, or 3rd pass...who knows, it's indeterminate
ActiveChatScreens.Remove(closingScreen);
// all clean up should be performed within Dispose method
chatWindow.Dispose();
//GC.Collect();
//GC.SuppressFinalize(this);
MessageBox.Show(GC.GetTotalMemory(true).ToString());
}
迫使垃圾的收集不建议。有办法工作与GC然而,这通常是在Dispose()方法的一次性类。你来ChatView对象的定义应该是这样的:
class ChatView : IChatViewModel, IDisposable
{ }
ChatView需要Dispose()方法以实施。还有一个 模式来遵守 (从MSDN) 当时创造的一次性类别:
// Design pattern for a base class.
public class ChatView : IChatViewModel, IDisposable
{
private bool disposed = false;
//Implement IDisposable.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Free other state (managed objects).
}
// Free your own state (unmanaged objects).
// Set large fields to null.
disposed = true;
}
}
// Use C# destructor syntax for finalization code.
~ChatView()
{
// Simply call Dispose(false).
Dispose (false);
}
}
不隶属于 StackOverflow