Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 531951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:21:16+00:00 2026-05-13T09:21:16+00:00

I have a WindowsForm with a panel control which I use to display my

  • 0

I have a WindowsForm with a panel control which I use to display my UserControls. I add controls this way:

private void AddControl(Control control)
{
    panel.Controls.Clear();
    control.Size = new Size(panel.Width - 1, panel.Height - 1);
    control.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles. Top;
    panel.Controls.Add(control);
}

..

AddControl(new ucSomeControl());

I just clicked every button that uses AddControl() and saw memory usage increased every time. I left the application running, doing nothing, for one and a half hour and the memory usage was dropped from 140mb to 138mb, just like 2mbs. Do you think this is normal or am I doing something wrong with my control adding method which I should/could improve for less memory usage?

Followup

I’ve created 4 versions of my application: Debug, Release, with Dispose, with manuel GC call.

With my original code

There’s little difference between debug and release versions of my application when it comes to memory usage, like 5mb. The problem with these versions is that the more I click on buttons, even if I click the same button and create the same UserControl again, memory usage equally increases.

With Dispose

I’ve added Chris Arnold’s Dispose code. Memory usage is significantly lower and although creating more and more controls still increases memory usage, now each control uses a lot less memory. This was a worthy addendum.

With manuel GC call

I’ve added this code after Dispose:

GC.Collect();
GC.WaitForPendingFinalizers();

Bingo! Even less memory usage than Dispose code. The best part is, even if I create new controls over and over again, the memory usage increases are very small, almost trivial.

I really liked using Dispose + GC method but every single article I’ve red about manuel GC calls are strongly discouraging its usage. Even if I don’t have any custom finalizers/destructors I’m undecided about using it or not..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:21:16+00:00Added an answer on May 13, 2026 at 9:21 am

    You could try disposing the Controls collection before you clear it. Like this…

    private void AddControl(Control control)
    {
        foreach (IDisposable control in panel.Controls)
          control.Dispose();
    
        panel.Controls.Clear();
        control.Size = new Size(panel.Width - 1, panel.Height - 1);
        control.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles. Top;
        panel.Controls.Add(control);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few listview controls on a windows form and i've assigned a
I have found this example on StackOverflow: var people = new List<Person> { new
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
I'm using C# and I have windows form and web service... I have a
I have a Windows Form that takes quite a bit of time to load
I have a Windows Form app written in C#. Its job is to send
I have a windows form application that uses a Shared class to house all
I have a pretty basic windows form app in .Net. All the code is
I have a simple .NET 2.0 windows form app that runs off of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.