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 6766067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:47:09+00:00 2026-05-26T14:47:09+00:00

I was experimenting with garbage collector and found strange count iCnt on destructor call.

  • 0

I was experimenting with garbage collector and found strange count iCnt on destructor call.
Here is the Code:-

public class MyClass
{
    static long iCnt;
    public MyClass()
    {
        iCnt++;
    }

    ~MyClass()
    {
        iCnt--;
    }
    static public string ObjectCount
    {
        get
        {
            return iCnt.ToString();
        }
    }
}

Below code is to create objects, delete objects(De-refernce by assigning to null) and explicitly call GC.Collect().
I have having all it in a Win Form which has Timer of interval = 100, timer show the current count of iCnt.

public partial class Form1 : Form
{
    MyClass[] Objs;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnCreateObjects_Click(object sender, EventArgs e)
    {
        Objs = new MyClass[1000];
        for (int i = 0; i < 1000; i++)
        {
            Objs[i] = new MyClass();
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label2.Text = MyClass.ObjectCount;
    }
    private void btnDeleteObjects_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 1000; i++)
        {
            Objs[i] = null;
        }
    }

    private void btnInvokeGC_Click(object sender, EventArgs e)
    {
        GC.Collect();
    }
}

Now create the object until the garbage collector is called automatically(for me it took 24 clicks). Again repeat this action.

I saw this iCnt < 1000. which keeps me confusing how the destructor called in this scenario.
when iCnt < 1000, click on delete object which deference the Objs[1000]. Then call GC.Collect() which makes count of iCnt < 0 (very strange).

Can anybody explain me this behavior. Thanks in advance.

  • 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-26T14:47:10+00:00Added an answer on May 26, 2026 at 2:47 pm

    This is a pretty classic threading bug. The destructor runs on the finalizer thread, asynchronously from the thread that increments the count. You need to use Interlocked.Increment() and Decrement() to do this safely. Or use the lock keyword.

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

Sidebar

Related Questions

While experimenting a bit with C++ templates I managed to produce this simple code,
Experimenting with new features of T-SQL, I've run into a puzzle. Here is some
after experimenting with js's prototypal inheritance i've found that i'm not wild about the
I've been experiencing occasional lags in my Android game when the garbage collector runs.
In experimenting with pickle, I've put together some code for a (very) simple blog.
Im experimenting with the following code private void timer1_Tick(object sender, EventArgs e) { Thread
Experimenting using different approaches and reducing code maintenance I have ended up using reflection
experimenting with Visitor pattern and generic method I found a kind of discrepancy in
After experimenting with an iterator block I noticed the generated IL code is not
Experimenting with the TcpClient and TcpListener class and for some reason when I have

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.