Click here to Skip to main content
15,886,857 members
Home / Discussions / C#
   

C#

 
AnswerRe: Try, Catch, Finally question Pin
ExcellentOrg5-Aug-13 22:06
ExcellentOrg5-Aug-13 22:06 
AnswerRe: Try, Catch, Finally question Pin
Bernhard Hiller5-Aug-13 22:46
Bernhard Hiller5-Aug-13 22:46 
GeneralRe: Try, Catch, Finally question Pin
JD866-Aug-13 6:18
JD866-Aug-13 6:18 
GeneralRe: Try, Catch, Finally question Pin
Jean A Brandelero8-Aug-13 10:47
Jean A Brandelero8-Aug-13 10:47 
QuestionClick-Once :: Publishing And Updates In Different Locations Pin
Matt U.5-Aug-13 8:17
Matt U.5-Aug-13 8:17 
AnswerRe: Click-Once :: Publishing And Updates In Different Locations Pin
Pete O'Hanlon6-Aug-13 23:05
mvePete O'Hanlon6-Aug-13 23:05 
GeneralRe: Click-Once :: Publishing And Updates In Different Locations Pin
Matt U.7-Aug-13 2:01
Matt U.7-Aug-13 2:01 
QuestionC# Garbage Collection and GC.Collect() Pin
DSLoginYea5-Aug-13 7:38
DSLoginYea5-Aug-13 7:38 
I was testing the garbage collection in C#, and came across something that doesn't make sense to me. I know about Garbage Collection happening "randomly", but I am using GC.Collect() here. Please see code below and then the result information:

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Method();
            Method2();

        }

        void Method()
        {

            TestA b = new TestA("first one");


            for (int i = 0; i < 10; i++)
            {
                TestA a = new TestA();
            }
            GC.Collect();
        }

        void Method2()
        {

        }

    }

    public class TestA
    {
        string MSG;

        public TestA()
        {
            MSG = "Not first one";
        }

        public TestA(string msg)
        {
            MSG = msg;
        }


        ~TestA()
        {

            MessageBox.Show("Destructing A " + MSG);

        }



    }



I am getting "Not first one" (destructor and I am assuming GC'ed) 9 times. Then when I close the form I am getting "first one" 1 time and then "Not first one" 1 more time.

My main question is why isn't instance b of TestA being collected on GC.Collect? Are the ones in the for loop falling out of scope, but not the one before my for loop? If so, please explain what is keeping it in scope as Method() has completed.

Thank you.

Respectfully,

D
AnswerRe: C# Garbage Collection and GC.Collect() Pin
Eddy Vluggen5-Aug-13 7:51
professionalEddy Vluggen5-Aug-13 7:51 
AnswerRe: C# Garbage Collection and GC.Collect() Pin
Abhinav S5-Aug-13 17:00
Abhinav S5-Aug-13 17:00 
AnswerRe: C# Garbage Collection and GC.Collect() Pin
Bernhard Hiller5-Aug-13 22:49
Bernhard Hiller5-Aug-13 22:49 
QuestionRun Windows project which is set up as a service Pin
vkEE5-Aug-13 2:37
vkEE5-Aug-13 2:37 
AnswerRe: Run Windows project which is set up as a service Pin
Richard MacCutchan5-Aug-13 3:18
mveRichard MacCutchan5-Aug-13 3:18 
AnswerRe: Run Windows project which is set up as a service Pin
Ron Nicholson5-Aug-13 3:39
professionalRon Nicholson5-Aug-13 3:39 
AnswerRe: Run Windows project which is set up as a service Pin
Bernhard Hiller5-Aug-13 22:53
Bernhard Hiller5-Aug-13 22:53 
Questioncreate hyberlink in string using RichTextBox control in WPF Pin
senthil kumar 2224-Aug-13 23:53
professionalsenthil kumar 2224-Aug-13 23:53 
AnswerRe: create hyberlink in string using RichTextBox control in WPF Pin
Richard MacCutchan5-Aug-13 1:05
mveRichard MacCutchan5-Aug-13 1:05 
Questiongridview inside gridview with insert update and delete Pin
vipulparmar904-Aug-13 22:49
vipulparmar904-Aug-13 22:49 
AnswerRe: gridview inside gridview with insert update and delete Pin
Mycroft Holmes4-Aug-13 23:02
professionalMycroft Holmes4-Aug-13 23:02 
AnswerRe: gridview inside gridview with insert update and delete Pin
madhubsu@gmail.com5-Aug-13 1:57
madhubsu@gmail.com5-Aug-13 1:57 
Questionhow to make backup and restore for ".mdf" file Pin
Awad Bekhet3-Aug-13 1:11
Awad Bekhet3-Aug-13 1:11 
AnswerRe: how to make backup and restore for ".mdf" file Pin
Code-Hunt3-Aug-13 1:55
Code-Hunt3-Aug-13 1:55 
GeneralRe: how to make backup and restore for ".mdf" file Pin
Awad Bekhet3-Aug-13 2:13
Awad Bekhet3-Aug-13 2:13 
GeneralRe: how to make backup and restore for ".mdf" file Pin
Code-Hunt3-Aug-13 2:19
Code-Hunt3-Aug-13 2:19 
GeneralRe: how to make backup and restore for ".mdf" file Pin
Awad Bekhet3-Aug-13 4:08
Awad Bekhet3-Aug-13 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.