1

I'm creating a Windows Forms app in C# (.NET 4.7.2), I just added a ToolStrip and a MenuStrip. If I run the application the memory usage jumps to 600mb, too much for a Windows Forms application with only a ToolStrip and a MenuStrip, if I delete them both memory usage returns to normal . I did some research and found that there is a memory leak in ToolStrip and MenuStrip, I also tried this (ToolStrip memory leak), but didn't work. How this can be fixed?

protected override void Dispose(bool disposing)
{

    if (disposing)
    {
        var overflow = toolStrip1.OverflowButton.DropDown as ToolStripOverflow;
        if (overflow != null)
            overflow.Dispose();
    }


    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}
  • Many things are cached, so the exact amount of "leak" is hard to determine. Use a profiler for that. – Jeroen van Langen Jul 19 '21 at 13:27
  • The Q&A you linked is from 2009. -- Did you add Images to your MenuStrip or ToolStrip items? Do you have images somewhere else? Note that compressed Images are decompressed when used in a UI, so a 500k image can turn into a monster, depending on how much the content can be compressed. Use `16x16` Images for your menus. – Jimi Jul 19 '21 at 13:51
  • I'm using 2 images in the menu, I'll try with no images – Francesco Granozio Jul 19 '21 at 13:58
  • Remove what you have added in the `Dispose()` override. Those components are already taken care of when `components.Dispose();` is called. – Jimi Jul 19 '21 at 14:08
  • I tried a 16 x 16 and 32 x 32 images and worked, thanks! – Francesco Granozio Jul 19 '21 at 14:14
  • I am having the same issue but my images are 16x16... was that your only issue? – MX313 Mar 31 '22 at 19:14

0 Answers0