Winform OpenFileDialog,everytime I open it,the memory will increase,dispose () and OpenFileDialog = null not working,the memory does not lose. .
how to fix this ??
private void btnLocalPicture_Click(object sender, EventArgs e)
{
OpenFileDialog ofdSelectPicture = new OpenFileDialog();
ofdSelectPicture.Filter = "PicFile|*.jpg;*.png;*.jpeg;*.gif;*.bmp;*.tif";
if (ofdSelectPicture.ShowDialog() == DialogResult.OK)
{
if (showPicture != null)
showPicture.Dispose();
showPicture = Image.FromFile(ofdSelectPicture.FileName);
if (pbShowPicture.Image != null)
pbShowPicture.Image.Dispose();
pbShowPicture.Image = showPicture;
path = ofdSelectPicture.FileName;
WordTip.Visible = false;
if (pbShowPicture.Image != null)
picOK.Enabled = true;
}
ofdSelectPicture.Dispose(); //not working
ofdSelectPicture = null; //not working
GC.Collect();
}
You can see what's going on with Project + Properties, Debug tab, tick the "Enable unmanaged debugging" option. Start your program and select the Output window. Get the dialog displayed.
You'll now see a list of DLLs getting loaded into your process. These are shell extensions registered on your machine. Exactly what you'll get is unpredictable, everybody has their own set of favorite extensions. Programmers tend to have a lot of them.
Yes, these extension will consume memory in your process. Just by the fact that these DLLs occupy space in your virtual memory address space. But also because these DLLs allocate memory for their own use. And a poorly written one can certainly leak memory. Beware that the memory allocated by these extensions is always unmanaged memory so be sure you have a good tool that shows you the leak. Something like TaskMgr.exe isn't good enough.
Two basic things you can do about it. First is to just ignore it, this problem is specific to your machine and your user won't have the same problem. You can't fix the leak, you don't have the source code for the extension. Or you can chase down the troublemaker with the SysInternals' AutoRuns utility. It shows you what shell extensions are registered and allows you to un-register them by clicking a checkbox.