Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
150
UltraGrid/DataTable memory usage
posted

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 

 

 

 

  • 570
    posted

     Hi !

     A good way to reduce the memory usage of a dot Net application is to minimize its working set. 

           

            [DllImport("kernel32.dll", EntryPoint="SetProcessWorkingSetSize", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
            private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);

            /// <summary>
            /// Reduce the working set size off the application to a minimum.
            /// </summary>
            public void ReduceWorkingSet() {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                try {
                    if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
                        String name = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
                        SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
                    }
                } catch (Exception e) {
                    Console.Out.WriteLine(e.Message);
                }               
            }

    Trimming the working set of an application will be also done by the operating system through the Minimize command from its System menu or a click on its Minimize button of the form (see: "The working set of an application is trimmed when its top-level window is minimized", http://support.microsoft.com/kb/293215/en-us)

     

    You may also use .net memory profiler tools (like http://memprofiler.com/) for optimizing applications and finding memory leaks.


    Regards,

    Claus

     

  • 150
    posted

    Thanks for the responses.

     I've gone through my code to reduce the amount of Row and Cell objects that get created, and this did reduce memory usage somewhat.

     I think my main culprit is probably my use of DataTables and not the grid. So at some point I might review what kind of data containers I use. The problem is, I need to generate columns dynamically at run time, so I don't think I can simply use a BindingList with a hardcoded class. The reason I chose to use the DataTable in the first place was that it allows me to add columns as I please. But now it seems there's a lot of overhead associated with using it, in terms of both cpu and memory usage.

     Thanks again for the input, this has definitely given me more insight.

  • 3707
    posted

    I get a similar effect with Microsoft programs as well. If you're watching your process in the task manager, minimize your application after it's finish loading up. You'll notice the huge decrease in memory usage. Maximize it again and you'll see it's not even half as big as it was when loading. I do this often when I'm bored and have Outlook or Visual Studio open.

  • 927
    posted

    Johan Nystrom said:

    ... 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.

    It's a lot of work, but you could implement a "virtual" data source. This is what I'm doing. I only load a chunk of data into my DataTable at a time. When the user scrolls the grid control up or down, I need to manage which rows of data get loaded into the DataTable so that the data can then be loaded into the grid control.