Opening a window in your own thread is not a good idea, usually only calculations are allocated to the stream. However, in this example, the memory will be freed if you subscribe to the Closed window event and call Dispatcher.CurrentDispatcher.BeginInvokeShutdown , and after closing the window call Dispatcher.Run . In actual use, Dispatcher.Run is called after the window is opened, and closing occurs by user action, not programmatically.
private void Something() { var t = new Thread(() => { var w = new Window() { Width = 1024, Height = 768 }; w.Show(); w.Closed += (s, ex) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background); Thread.Sleep(1000); w.Close(); Dispatcher.Run(); }); t.SetApartmentState(ApartmentState.STA); t.Priority = ThreadPriority.Normal; t.Start(); GC.Collect(); }
Information from here .