La exportación con el MEF en Caliburn.Micro - El aumento de problemas de memoria
-
25-09-2019 - |
Pregunta
Tengo un problema con el aumento de la memoria. Yo uso MEF en caliburn.micro en la creación de nueva pantalla -. Ventana de WPF
Ver modelo de pantalla / vista este aspecto:
[Export(typeof(IChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IChatViewModel
{}
En la creación utilizo ExportFactory, controladora está aquí:
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();
}
}
Yo uso la clase ViewModelsControler en clase ChatScreenManager. Esta clase abierta pantalla de chat / Eliminar.
Aquí es:
[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());
}
}
Y mi problema es:
- Me llamo método OpneChatScreen de ChatScreenManager que abrir una nueva ventana de WPF
- Agregar referencia en esta ventana al diccionario.
- Cuando estoy cerrando la ventana que llamo RemoveChatScreen.
En RemoveChaScreen:
- consigo memoria total, por ejemplo, es que 37,000K
- Luego llamada al método Dipose en ExportLifetimeContext chatScreen
- Fuerza GC
- Y conseguir memoria total, por ejemplo, es que 39,000K
La memoria es stil en aumento. Espero que si llamo método Dispose en el objeto y el objeto ChatViewModel también ChatView estos objetos son destruidos.
Solución
No forzar GC! Además, el método Dispose()
debe seguir la eliminación de su colección.
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());
}
No se recomienda forzar la recolección de basura. Hay maneras de trabajar con GC, sin embargo, y que por lo general se hace en el método Dispose () de la clase desechable. Su objeto ChatView derivados se debe definir algo como:
class ChatView : IChatViewModel, IDisposable
{ }
ChatView requiere ser implementado un método Dispose (). Hay un patrón seguir < em> (de MSDN) cuando la creación de clases desechables:
// 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);
}
}