Home > Net >  WPF window is turned off, why not release the memory
WPF window is turned off, why not release the memory

Time:04-16

1. With the main window. A, only A Button
B 2. And the child window, there are 100 TextBox control
3. In the window A, there is A loop, create and display (ShowDialog) window B
4. Window B displayed immediately after the Close themselves

B is turned off, the question is: window memory and not recycling, there was an OutOfMemoryException until the 975th cycle is unusual,
Don't understand why such problems, in the code and not move managed object,
Consult how to solve this problem?

Below is the code:
A window: (MainWindow)
 & lt; Windows x: 
XMLNS="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
XMLNS: x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" & gt;






///& lt; Summary> 
///MainWindow. Xaml interaction logic
///& lt;/summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
}

Private void Button_Click (object sender, RoutedEventArgs e)
{
System. Threading. Tasks. Task. Factory. StartNew (()=& gt;
{
var num=0;
Var count=0;
While (true)
{
This. The Dispatcher. Invoke ((Action) (()=& gt;
{
Try
{
Var v=new TestWindow {Owner=this};
V. howDialog ();

((Button) sender). The Content=num;
Count=Application. Current. Windows. Count;

//GC. Collect ();//invalid
//GC. WaitForPendingFinalizers ();//invalid

Thread.sleep (1);
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);
}
}));

Console. WriteLine (the string. Format (Num={0} "Count={1}", num++, Count));
}
});
}
}


B: (TestWindow window)
 & lt; Windows x: 
XMLNS="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
XMLNS: x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestWindow" WindowStartupLocation="CenterScreen" Height="300" Width="300" ContentRendered="Window_ContentRendered & gt;"




































































  • Related