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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:04:56+00:00 2026-05-23T05:04:56+00:00

I read some informations about Garbage Collection (how it’s works etc.). I tried understand

  • 0

I read some informations about Garbage Collection (how it’s works etc.). I tried understand how it’s working doing my examples but I think I have problem. I know Garbage Collector runs when:
is not enough memory,
you call GC.Collect().
This is my code:

public partial class Form1 : Form
{
    public Testing _d;
    public Boolean _first = false;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (!_first)
        {
            _d = new Testing();
            int test = _d.DoSomething("example");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        _first = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        //if (_first)
        //{
        //    _d = null;
        //}
        GC.Collect();
    }
}

public class Testing
{
    private ASCIIEncoding _ascii;
    private bool _disposed = false;

    public Testing()
    {
        _ascii = new ASCIIEncoding();
    }

    public int DoSomething(string message)
    {
        return _ascii.GetByteCount(message);
    }
}

When I click button1 I’m creating new object Testing. _d is reference to this new object. I’m dumping memory using JetBrains dotTrace Memory and see this new object exists. After click button2 I’m setting boolean _first to true in order to _d became unreachable. At this point I thought when I run GC.Collect() GC will ‘clear’ this object from stack, but I see it’s still exists. I misunderstood about GC work? or I’m doing this wrong?
It’s working when I set _d = null;

  • 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-23T05:04:57+00:00Added an answer on May 23, 2026 at 5:04 am

    Setting first=false does not make the _d instance unreachable in terms of the the GC. Logically you may never use it again but it is still referenced in the Form1 class.

    If someone were to set first=true again, would you not expect the object to still be usable?

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

Sidebar

Related Questions

I read some information about named branches and working with bookmarks. Unfortunately I still
I read some articles about Comet tech. All of them mentioned that the long-life
I read some tutorials concerning Makefiles but for me it is still unclear for
I read some microsoft articles.They explained that WCF uses DataContractSerializer for serialization.But the articles
I need to save a few informations about some files. Nothing too fancy so
My application needs to log informations about user actions (inserts, updates, deletes, etc) and
I read many and many and many articles on WCF security etc... but i'm
I'm thinking about security problems, made some questions, read some articles..... it's too difficult
I've read some good information (on SO) about how you must avoid storing credit
I've just read up some information on Vector and ArrayList . From what I

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.