gpt4 book ai didi

c# - System.Drawing.Internal.GPStream 引起的内存泄漏

转载 作者:行者123 更新时间:2023-11-30 17:48:49 26 4
gpt4 key购买 nike

每次我打开和关闭某个表单时,我的应用程序的大小都会不断增加。我使用 dotTrace 并提出了一个不断增长的 System.Drawing.Internal.GPStream 类型的对象列表,这些对象连续创建字节数组,而不是每次都处理它们。我做了一些研究,发现 .net 不支持任何关闭此类内存流的方法。以下

foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
{
if (t.IsClass && t.BaseType.Name.ToLower() == "form")
{
//Assembly ass = Assembly.GetExecutingAssembly();
object obj = null;
obj = Activator.CreateInstance(t);
Form f = new Form();
f = (Form)obj;
if ((string)f.Tag != "DNI") // Do Not Import
{
DataRow dr = Formsdt.NewRow();
dr["Name"] = f.Name;
dr["Text"] = f.Text;
dr["Tag"] = f.Tag;
Formsdt.Rows.Add(dr);
}
}
}

此代码的目的是遍历所有表单并检索控件,以便为用户设置访问权限,根据需要以编程方式激活和停用控件。

任何见解都值得赞赏。注意虽然(我正在打开和关闭表格)==>内存分配不断增加

最佳答案

有些东西没有被妥善处理。你有警告说 X 类是 iDisposable 而你没有处理它吗?考虑使用“使用” block 。

看这个:

"A FileStream involves unmanaged resources which could actually be immediately freed upon calling Dispose. A MemoryStream, on the other hand, stores a managed byte array in its _buffer variable, which is not freed at disposal time. In fact, the _buffer is not even nulled in the MemoryStream's Dispose method, which is a SHAMEFUL BUG IMO because nulling the reference could make the memory eligible for GC right at disposal time. Instead, a lingering (but disposed) MemoryStream reference still holds onto memory. Therefore, once you dispose it, you should also null it if it's still in scope." – Triynko Oct 25 '10 at 20:46

Is a memory leak created if a MemoryStream in .NET is not closed?

关于c# - System.Drawing.Internal.GPStream 引起的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22405030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com