【发布时间】:2012-07-29 11:14:08
【问题描述】:
Winform OpenFileDialog,每次打开,内存都会增加,dispose()和OpenFileDialog=null都不行,内存不会丢失。 .
如何解决这个问题??
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();
}
【问题讨论】:
-
你是如何测量内存消耗的?因为 gc 不是确定性的,也不一定会将内存释放回操作系统,所以只有内存分析器才能让您真实了解持有内存分配的引用是否仍然是根目录。
-
这取决于你是否发现了内存泄漏,或者这是否是 gc 的正常操作。在决定是否真的有问题之前,您需要进行有效的测量。你怎么测???当然,有问题的内存量可以忽略不计吗?您打算打开多少个文件对话框?
-
你不应该(必须)调用 GC.Collect()。
-
如果打开文件对话框导致加载额外的dll,那么在卸载appdomain之前不会释放这块内存
-
你确定这不是你看到的
Image的内存吗?每次打开新图片时,内存消耗都会无限增加吗?还是仅在第一张图片之后?
标签: c# openfiledialog