Hi all,
This is something of a broad question, and I'd appreciate any experience or advice people might have.
I'm finding that the UltraGrid can sometimes consume a lot of memory. I'm loading data from raw text files (50 MB in total) and displaying the data as columns of doubles and strings in several grids. I've seen the memory usage of my program shoot up to approximately 500 MB. Memory profiling reveals that most of the data is string arrays and double arrays from the grid and its data structures (but mainly string arrays).
The grids are backed by DataTables that I generate myself.
So my question is, what would be some good advice for keeping the memory usage of the grid down?
A second question, which perhaps is outside of the scope of this forum, is what can be done to keep the memory usage of the System.Data.DataTable down. The single best thing I've found was to set the initial capacity of the table to reduce the amount of memory it allocates. Normally it seems to double the amount of rows allocated each time it runs out of memory, so setting the exact capacity from the start was a big help.
Any advice/ideas would be appreciated.
Many thanks,
Johan Nystrom
Hi Johan,
I can't think of any reason why the grid would be using string or double arrays internally. The grid doesn't store any of the data - it relies on the data source for that. It may keep a few strings in memory for display on the screen, but it certainly doesn't keep a copy of the entire set of data.
The most common cause of the grid using up extra memory is the creation of cells. As you might imagine, if you have a grid with a lot of rows and columns, the sheer number of cells can add up pretty quickly. This is why grid creates cell objects lazily. So one thing you should try to do is avoid the unneccessary creation of UltraGridCell object. If you are looping through the grid rows and accessing cells or maybe accessing cells inside the InitializeRow event, this will cause a lot of memory to be used. You can usually avoid this by setting properties on the column or using methods on the row whenever possible rather than accessing a cell.
Another way to make the grid more efficient is to use the CellDisplayStyle property on the colmun. The documentation on this property explains what it does and how to use it, so I won't repeat it all here. But basically, it can help prevent the cell from using UIElements that may not be neccessary.
Regarding the efficiency of a DataSet/DataTable, the best thing I recommend is - don't use them. :)
In my experience, DataSets are DataTable are not very efficient either in memory usage or performance. If you can, use a different data source like UltraDataSource or a custom data source, or even a BindingList<>.