Managing .NET DataGridView uses TON memory for me

I am currently using a DataGridView control in my application, and I am facing this problem. I don't use DataBinding at all, just a manual population. The data that I put in them is not so much. Usually we talk about 20 rows of 20 columns of data.

The workflow for this control is that I have a ComboBox with different datasets that I load when you select them. Thus, the general use case moves cyclically through the data sets in the comboBox and sees their visualization in the dataGrid. What I see is that when people cycle through datasets, the memory usage of the application instantly goes from 100 MB to 1100 MB, and then drops down when the GC starts working. But if you quickly go through the datasets (i.e. faster than the GC's outputs), you will run out of memory and the application will die.

After debugging, I found that the reason MAIN, why the memory jumps up like crazy, is because I have several columns in this datagrid that are related to the image type. I use them to display a 16x16 icon that indicates the status for this line. These icons are stored in an ImageList, and I just set them as the value for this cell when I fill the rows of the data grid. If I take out the images and replace them with plain text, I absolutely do not see a burst of memory.

So what is the deal? Why are tiny 16x16 images making the control go crazy with my mind?

Additional Information:

My logic when switching datasets:

  • Clear all grid lines: dataGrid.Rows.Clear ();
  • Clear all columns: dataGrid.Columns.Clear ();
  • Add columns to the dataGrid control: (most row types and some DataGridViewImageColumn)
  • Add the data that I use line by line using dataGrid.Rows.Add (object [] data); (data includes images that I need to use as icons).
+4
source share
5 answers

The DataGridView control is designed to work with a DataBinding. If you are not using a DataBinding at all, I suggest you use a ListView control.

ListView is optimized for displaying images. In addition, it is easier to create an instance in memory than a DataGridView. In addition, the ListView control allows you to change the view:

  • ListView.View = View.Details;
  • ListView.View = View.LargeIcons;
  • ListView.View = View.List;
  • ListView.View = View.SmallIcons;
  • ListView.View = View.Tile.

These are the types that may occur in your Windows Explorer. You only need to define yourself as LargeIcons ImageList or SmallIcons ImageList, or one of them, depending on the view that allows your user to see.

You can also display GridLines as if it were a DataGridView, just to give the same aspect.

+1
source

In your third paragraph, you say that you have your own icons in ImageList, but in your bulleted list of logical steps, you say that you add the images you need as icons in the form of an array of objects. Could ImageList added to dataGrid be to blame?

0
source

By the way, I decided to do it differently. I resorted to using Custom Painting to paint my icons in a cell. It turned out much better. This grid no longer uses memory with this solution.

Using ListView was possible, but not very practical, since it only supports placing icons in the first column. So in any case, I would have to do the usual painting.

0
source

I ran into this error and seems to be triggered by a link to an image from ImageList. To get around this, I created images displayed as content in the project. Then I set the image values ​​to datatable using Bitmap.FromImage ("images / test.png") and this odd memory error went away.

0
source

Datagridviews have a memory leak when it comes to images. Before loading the data, set the datagridview data source to null, then run the dispose command (with GC.Collect ()) on the data source.

0
source

Source: https://habr.com/ru/post/1302830/


All Articles