1

I made a control by myself to "fake" a detail grid. Every row will be a single control which means i need a lot of them. I'm trying to store them in a StackPanel which is located in a ScrollViewer.

When i add a control the memory usage of my executable climbs up 10mb. When i am trying to use to draw all 110 data packages it is climbing up to 1.5GB and throws the OutOfMemory exception.

Control is very minimalistic.. no executions, just a few labels, some vector graphics, an expander and a set of 3 more controls (only multiple labels).

How can i solve this problem?

Noam M
  • 3,156
  • 5
  • 26
  • 41
Marc
  • 103
  • 17
  • why do you have to "Fake" a grid in the first place? – psoshmo Jul 07 '15 at 12:59
  • Can you post the control? I've done something similar (although with more minimalist control) without any problems showing +thousand of it – Diego O.d.L Jul 07 '15 at 13:08
  • 1
    I would recommend implementing Virtualization in your custom control to ensure it is only rendering visible items (it will re-use the controls as you scroll and replace the DataContext behind them), and looking into using a basic cell template for all items except the one that currently has user focus. A basic example of something I once did a while back to decrease memory use in a Grid was to display every item as a basic TextBlock, and only replace it with a TextBox when the user sets focus on the cell. – Rachel Jul 07 '15 at 14:41
  • Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a *specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – Federico Berasategui Jul 07 '15 at 15:42
  • @Rachel: Virtualization's not my favourite solution but i will try this out. – Marc Jul 08 '15 at 07:01
  • I found out what allocates that much memory: I have the gridrow-like control which holds 3 container-controls which holds about 15 controls per. I reduced the amount and the memory was only at 170mb. So i will reduce that amout of controls by adding the content to the parent control easily and load that controls only if the gridrow-like control is expanded (expandable gridrow / detail grid view). Thanks to all of you! – Marc Jul 08 '15 at 07:04

2 Answers2

2

As a quick fix you can try this code which will immediately releases memory

 /// <summary>
    /// Memory Management
    /// </summary>
    public class MemoryManagement
    {
        /// <summary>
        /// Clear un wanted memory
        /// </summary>
        public static void FlushMemory()
        {
            try
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
                }
            }
            catch (Exception e)
            {
            }
        }

        /// <summary>
        /// set process working size
        /// </summary>
        /// <param name="process">Gets process</param>
        /// <param name="minimumWorkingSetSize">Gets minimum working size</param>
        /// <param name="maximumWorkingSetSize">Gets maximum working size</param>
        /// <returns>Returns value</returns>
        [DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet =
          CharSet.Ansi, SetLastError = true)]
        private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
    }
Justin CI
  • 2,693
  • 1
  • 16
  • 34
1

It seems like what you're trying to do is to have a certain template for each of your cells. You should use item controls and their template to help you achieve this instead, take a look here is there a datatemplate for grid panel elements in WPF?, here WPF - Display single entity with a data template and here ItemsControl ItemTemplate Binding for examples and more info on how to use them.

Community
  • 1
  • 1