سؤال

I have an app where I dynamically create user controls depending on the situation. When I'm done with them I remove them from the container, but they are still taking up memory.

I've implemented a "dispose" method in the code to clear the controls inside of the user control, but I still feel like there is a better way to do it.

I'm not sure what code I can post as no code is really relevant.

All I really have is what I'm doing now with the dispose method

  public void Dispose()
        {

            ContentWebView.NavigateToString("about:blank");
            YoutubeMediaPlayer.Stop();
        }
هل كانت مفيدة؟

المحلول

The Dispose method is used for releasing unmanaged resources.

When all references to an object are removed, the garbage collector will eventually "Collect" it (freeing the memory). You can't actually control when this happens, even calling GC.Collect is technically just a suggestion. You can, however, invoke dispose on an object directly (note that this may cause odd behavior or exceptions if it is still in use!)

If you suspect a control has unmanaged resources that need to be cleaned up, you should call Dispose on it directly when you are finished with it (GDI Bitmap objects need to be cleaned up like this). From your comments, it sounds like you just need the garbage collector to actually free the memory, which is something thats hard to control (you can try calling GC.Collect though).

MSDN for Dispose: http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top