Domanda

Ho problemi con l'aumento della memoria. Io uso MEF in caliburn.micro sulla creazione di nuova schermata -. Finestra WPF

Visualizza il modello di schermo / vista simile a questa:

[Export(typeof(IChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IChatViewModel
    {}

Sulla creazione che uso ExportFactory, controler è qui:

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();
    }
}

Io uso di classe in classe ViewModelsControler ChatScreenManager. Questa classe aperta / Rimuovi schermata della chat.

Qui è:

  [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());
        }
    }

E il mio problema è il seguente:

  • chiamo metodo OpneChatScreen da ChatScreenManager si apre una nuova finestra WPF
  • Aggiungi riferimento in questa finestra al dizionario.
  • Quando sto chiudendo la finestra chiamo RemoveChatScreen.

In RemoveChaScreen:

  • ottengo memoria totale, ad esempio, è 37.000 migliaia
  • Poi ho chiamare il metodo dipose su ExportLifetimeContext chatScreen
  • Forza GC
  • E ottenere memoria totale, ad esempio, è 39,000K
utilizzo

La memoria è stil crescente. Spero che se chiamo metodo Dispose sull'oggetto ChatViewModel e oggetto anche ChatView questi oggetti vengono distrutti.

È stato utile?

Soluzione

Non forzare GC! Inoltre, il metodo Dispose() dovrebbe seguire la rimozione dalla tua collezione.

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());
}

Non è consigliabile Forzare garbage collection. Ci sono modi di lavorare con GC, tuttavia, e che è tipicamente effettuata nel metodo Dispose () della classe monouso. Il vostro oggetto ChatView derivato dovrebbe essere definito qualcosa di simile:

class ChatView : IChatViewModel, IDisposable
{  }

ChatView richiede un metodo Dispose () attuato. C'è un modello da seguire < em> (da MSDN) durante la creazione di classi monouso:

// 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);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top