Today, we found that the newly written WinForm application occupies abnormal memory. After analysis and testing, we found that it was caused by multiple WebBrowser controls. To facilitate problem description, the following is the simplified code:
Form1.cs:
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
(New Form2 (). Show ();
}
}
Form2.cs:
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
Private void Form2_Load (object sender, EventArgs e)
{
WebBrowser1.Navigate ("http://www.111cn.net /");
}
}
Refer to the following descriptions on MSDN:
Reference content
The WebBrowser control occupies a large amount of resources. After using this control, you must call the Dispose () method to release all resources in time. The Dispose () method must be called on the same thread that attaches an event. This thread should always be a message or user interface (UI) thread.
However, the actual test of the Dispose method does not reduce the memory. At present, no good solution is found. After the test, it is found that different pages are opened in the same WebBrowser control, and the memory remains within a certain range, therefore, avoid creating the WebBrowser control multiple times and try to open the page in the same WebBrowser control.