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

  • SEARCH
  • Home
  • 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 9218525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:53:05+00:00 2026-06-18T02:53:05+00:00

void Foo() { System.Windows.Forms.Form f = new System.Windows.Forms.Form(); f.Show(); } To my understanding f

  • 0
void Foo()
{
     System.Windows.Forms.Form f = new System.Windows.Forms.Form();
     f.Show();
}

To my understanding f holds the reference to the Form. But f is a local variable and it will go out of scope when the control leaves the curly braces. But the Form is still open. I tried calling GC.Collect(), but the Form is still open.

One more scenario.

private void button2_Click(object sender, EventArgs e)
    {
        Timer t = new Timer();
        t.Enabled = true;
        t.Interval = 1000;
        t.Tick += new EventHandler(t_Tick);
    }

    void t_Tick(object sender, EventArgs e)
    {

    }

In this scenario the t is never getting garbage collected. After a lot of research, I found Timer class is requesting GC not to collect using – GCHandle.Alloc when I set t.Enabled = true. Guys, this is a big source of memory leak. Unless I set t.Enabled = false, the whole Form will be leaked even after we close the Form.

In the first example code, I could not understand why the Form didn’t get garbage collected even after I trigger a GC.Collect(). In reflector I saw ControlNativeWindow has been used in Form which internally uses GCHandle.Alloc. Is that a reason?. As a user of the .NET library, I always believed that when a reference goes not reachable, it will get a chance for garbage collection. Of-course garbage collection and the actual release from memory is un-deterministic. But my question here is – Is my understanding correct for both the examples? When there are objects that can live even after it goes not-reachable, then how will I track that to prevent memory leak?

  • 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-06-18T02:53:06+00:00Added an answer on June 18, 2026 at 2:53 am

    Winforms keeps an internal table that maps handles to control instances. That table ensures that a control (the form in your case) can never be garbage collected as long as the native window is alive. It gets removed from that table when the window is destroyed, either by the user closing the form or your code disposing it.

    The System.Timers.Timer is kept alive by a cookie that’s referenced by the CLR. The class is implemented with a System.Threading.Timer which has a constructor that takes a state object argument. That state object is the cookie, the CLR keeps it referenced with the equivalent of GCHandle.Alloc(). Disabling the timer resets the cookie which allows the timer to be garbage collected.

    These are natural and necessary ways that the framework prevents these objects from getting garbage collected too early. You can only cause a leak by forgetting to disable the timer when the form is disposed. Which in general is quite unhealthy, you don’t want a timer to keep ticking when the form is dead. Move the Dispose method from the Designer.cs file into the form code or override OnFormClosed() to disable the timer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class a{ public void foo(int a){ System.out.println(super); } } class b extends a{ public
Given a ParameterInfo p from this: void foo(int modopt(IsLong) n); p.GetOptionalCustomModifiers() returns a System.Runtime.CompilerServices.IsLong;
public class Foo { public static void main(String[] args) { float f; System.out.println(f); }
void Foo() { string ID = test; var testctrl = new Control() {ID =
void Foo(Type^ type) { System::Guid id = type->GUID; switch (id) { case System::Byte::typeid->GUID: ...
Code: class TestA { public void foo(String... strings ) { System.out.println(TestA::foo); } public void
Assume that I have these three classes: class Foo { void fn() { System.out.println(fn
Does anyone know why: public void foo() { System.out.println(Hello); return; System.out.println(World!); } Would be
public class A { public void foo() { System.out.println(A's foo); } } public class
void foo(Node* p[], int size){ _uint64 arr_of_values[_MAX_THREADS]; for (int i=0 ; i < size

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.