C#WinForms DataGridView OutOfMemoryException在创建多个表单之后->;未由GC收集
我有一个带有DataGridView的表单。我多次打开和关闭(创建和处置)表单,这会导致内存问题。GC没有收集它,经过几个小时的工作,OutOfMemoryException发生 给定的表单有两个组件。我试图在没有组件的情况下复制我的问题,并将问题缩小到一个。这个有问题的有3个DataGridView。当我删除这些DGV时,我的问题就消失了 我试图检查DGV不会导致“内存泄漏”的任何情况(我不确定是否可以这样称呼它),但我找不到任何情况 我甚至试过C#WinForms DataGridView OutOfMemoryException在创建多个表单之后->;未由GC收集,c#,datagridview,memory-leaks,garbage-collection,out-of-memory,C#,Datagridview,Memory Leaks,Garbage Collection,Out Of Memory,我有一个带有DataGridView的表单。我多次打开和关闭(创建和处置)表单,这会导致内存问题。GC没有收集它,经过几个小时的工作,OutOfMemoryException发生 给定的表单有两个组件。我试图在没有组件的情况下复制我的问题,并将问题缩小到一个。这个有问题的有3个DataGridView。当我删除这些DGV时,我的问题就消失了 我试图检查DGV不会导致“内存泄漏”的任何情况(我不确定是否可以这样称呼它),但我找不到任何情况 我甚至试过 for (int index = 0; ind
for (int index = 0; index < 2000000000; index++)
{
var dataGridView = new DataGridView();
dataGridView.Dispose();
Console.WriteLine(index.ToString());
}
一切看起来都很好,内存被释放了,但这并不优雅,不正确,基本上就是不知道您的.net framework版本和操作系统是什么?Windows 10(最新)和.NetFramework 4.5.2编辑:由于某些硬件需求,我无法更新它。只是需要重新编程。在4.7.2中,使用网格(以及填充数据适配器)并移除dispose()方法体!!!内存利用率稳定您是否可以在VS诊断工具选项卡中共享测试循环期间捕获的流程内存图表的屏幕截图?我想知道你的GC收集频率是多少,用黄色的小楔子表示。或者,您可以执行堆快照并查看哪些对象消耗大量内存。尝试在发布模式下重建应用程序,并测试其行为是否相同。调试:发布:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
for (int index = 0; index < 2000000000; index++)
{
var form1 = new Form1();
form1.Dispose();
Console.WriteLine(index.ToString());
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(205, 116);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(240, 150);
this.dataGridView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}