-
Notifications
You must be signed in to change notification settings - Fork 949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataGridView memory usage #3376
Comments
|
I'm not sure this is the most efficient way of doing this. The reason the memory is shooting up so much is because you are creating a new row for every single element in the list in this check: private void button2_Click(object sender, EventArgs e)
{
int i1 = dataGridView1.Rows.Count;
if (i1 > 10)
{
button1.Enabled = false;
button2.Enabled = false;
string s1 = dataGridView1.Rows[i1 - 10].Cells[0].Value.ToString();
var sw = Stopwatch.StartNew();
for (int i = 0; i < i1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == s1)
{
dataGridView1.CurrentCell = dataGridView1[0, i];
dataGridView1.CurrentCell.Selected = true;
break;
}
}
textBox1.Text = sw.ElapsedMilliseconds.ToString() + " ms";
button1.Enabled = true;
button2.Enabled = true;
}
} If you look at the implementation, you can see that we clone a shared row each time we access it. See https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewrow?view=netcore-3.1
public DataGridViewRow this[int index]
{
get
{
DataGridViewRow dataGridViewRow = SharedRow(index);
if (dataGridViewRow.Index == -1)
{
if (index == 0 && items.Count == 1)
{
// The only row present in the grid gets unshared.
// Simply update the index and return the current row without cloning it.
dataGridViewRow.Index = 0;
dataGridViewRow.State = SharedRowState(0);
if (DataGridView != null)
{
DataGridView.OnRowUnshared(dataGridViewRow);
}
return dataGridViewRow;
}
// unshare row
DataGridViewRow newDataGridViewRow = (DataGridViewRow)dataGridViewRow.Clone();
newDataGridViewRow.Index = index;
newDataGridViewRow.DataGridView = dataGridViewRow.DataGridView;
newDataGridViewRow.State = SharedRowState(index);
SharedList[index] = newDataGridViewRow;
int columnIndex = 0;
foreach (DataGridViewCell dataGridViewCell in newDataGridViewRow.Cells)
{
dataGridViewCell.DataGridView = dataGridViewRow.DataGridView;
dataGridViewCell.OwningRow = newDataGridViewRow;
dataGridViewCell.OwningColumn = DataGridView.Columns[columnIndex];
columnIndex++;
}
if (newDataGridViewRow.HasHeaderCell)
{
newDataGridViewRow.HeaderCell.DataGridView = dataGridViewRow.DataGridView;
newDataGridViewRow.HeaderCell.OwningRow = newDataGridViewRow;
}
if (DataGridView != null)
{
DataGridView.OnRowUnshared(newDataGridViewRow);
}
return newDataGridViewRow;
}
else
{
return dataGridViewRow;
}
}
} Your solution could be:
This cuts the running time from over 30s to 500ms on my machine As to why memory permanently increases, I assume maybe |
Thank you, Thank you!! |
.NET Core 3.1.4
DataGridView memory usage so much and the first search takes too long time.
Example:
DGTest.ZIP
The text was updated successfully, but these errors were encountered: