Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Garbage Collector

Garbage Collector

Scheduled Pinned Locked Moved C#
questioncareer
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ashwani_kumar
    wrote on last edited by
    #1

    Hi Somebody has asked me a question in interview. we have two classes Class1 and Class2 public class Class1 { private Class2 obj; public void method1() { obj = new Class2(); } public void method2() { obj.method1(); } } public class Class2 { public Class2() { GC.SuppressFinalize(this); } public void method1() { } } Now in GUI form: onbutton_Click I write the follwing code: Class1 class1 = new Class1(); class1.method1(); class1.method2(); After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?

    Ashwani

    M C D 3 Replies Last reply
    0
    • A Ashwani_kumar

      Hi Somebody has asked me a question in interview. we have two classes Class1 and Class2 public class Class1 { private Class2 obj; public void method1() { obj = new Class2(); } public void method2() { obj.method1(); } } public class Class2 { public Class2() { GC.SuppressFinalize(this); } public void method1() { } } Now in GUI form: onbutton_Click I write the follwing code: Class1 class1 = new Class1(); class1.method1(); class1.method2(); After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?

      Ashwani

      M Offline
      M Offline
      MCSD Gandalf
      wrote on last edited by
      #2

      Ashwani, Of course the easy answer is YES...the garbage collector will eventually get everything sooner or later(even if it has to wait for a reboot :laugh: ). The better question, and the one they probably intended was, "Are the objects of Class1 and Class2 marked for GC or not?". The answer is still yes. I assume the bold on the GC.SuppressFinalize(this); line is primarily what they are asking about. That line HAS NO MEANING WHATSOEVER IN THIS CONTEXT. That line is meant to short circuit the running of a destructor/finalizer for a class. Since neither of the classes has a destructor defined, the line of code does nothing. In this case both classes will be marked for GC at the end of the click event code. HTH WhiteWizard(aka Gandalf)

      A 1 Reply Last reply
      0
      • M MCSD Gandalf

        Ashwani, Of course the easy answer is YES...the garbage collector will eventually get everything sooner or later(even if it has to wait for a reboot :laugh: ). The better question, and the one they probably intended was, "Are the objects of Class1 and Class2 marked for GC or not?". The answer is still yes. I assume the bold on the GC.SuppressFinalize(this); line is primarily what they are asking about. That line HAS NO MEANING WHATSOEVER IN THIS CONTEXT. That line is meant to short circuit the running of a destructor/finalizer for a class. Since neither of the classes has a destructor defined, the line of code does nothing. In this case both classes will be marked for GC at the end of the click event code. HTH WhiteWizard(aka Gandalf)

        A Offline
        A Offline
        Ashwani_kumar
        wrote on last edited by
        #3

        Thanks for your reply. This is what I have answered in the interview. But the interviewer was not agree with me. Can I see it visually(When these objects actually collected by GC) using any tool provided by Microsoft or by any other company.

        Ashwani

        Mircea PuiuM C 2 Replies Last reply
        0
        • A Ashwani_kumar

          Thanks for your reply. This is what I have answered in the interview. But the interviewer was not agree with me. Can I see it visually(When these objects actually collected by GC) using any tool provided by Microsoft or by any other company.

          Ashwani

          Mircea PuiuM Offline
          Mircea PuiuM Offline
          Mircea Puiu
          wrote on last edited by
          #4

          Someone could hate interviews, especially when the interviewers are not programmers :doh:

          SkyWalker

          1 Reply Last reply
          0
          • A Ashwani_kumar

            Hi Somebody has asked me a question in interview. we have two classes Class1 and Class2 public class Class1 { private Class2 obj; public void method1() { obj = new Class2(); } public void method2() { obj.method1(); } } public class Class2 { public Class2() { GC.SuppressFinalize(this); } public void method1() { } } Now in GUI form: onbutton_Click I write the follwing code: Class1 class1 = new Class1(); class1.method1(); class1.method2(); After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?

            Ashwani

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Ashwani_kumar wrote:

            After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?

            Yes. It will garbage collect instances of class1 and class2.


            Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

            1 Reply Last reply
            0
            • A Ashwani_kumar

              Hi Somebody has asked me a question in interview. we have two classes Class1 and Class2 public class Class1 { private Class2 obj; public void method1() { obj = new Class2(); } public void method2() { obj.method1(); } } public class Class2 { public Class2() { GC.SuppressFinalize(this); } public void method1() { } } Now in GUI form: onbutton_Click I write the follwing code: Class1 class1 = new Class1(); class1.method1(); class1.method2(); After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?

              Ashwani

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Yes, it will. SuppressFinalize just tells the GC to remove the object from the finalization queue. It still gets collected. The interviewer told you you were wrong?? Doesn't sound like a company I'd want to work for then...

              Dave Kreskowiak Microsoft MVP - Visual Basic

              1 Reply Last reply
              0
              • A Ashwani_kumar

                Thanks for your reply. This is what I have answered in the interview. But the interviewer was not agree with me. Can I see it visually(When these objects actually collected by GC) using any tool provided by Microsoft or by any other company.

                Ashwani

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Ashwani_kumar wrote:

                This is what I have answered in the interview. But the interviewer was not agree with me.

                Then the interviewer is an idiot. If the interviewer is going to ask question they must do adequate research that they, at the very least, don't look like a complete fool. If they offer you the job, think very carefully about whether you actually want to work there or not. Some people get very defensive if you tell them they are wrong. Remember: The mind is like a parachute; it works best when it is open.


                Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                D 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  Ashwani_kumar wrote:

                  This is what I have answered in the interview. But the interviewer was not agree with me.

                  Then the interviewer is an idiot. If the interviewer is going to ask question they must do adequate research that they, at the very least, don't look like a complete fool. If they offer you the job, think very carefully about whether you actually want to work there or not. Some people get very defensive if you tell them they are wrong. Remember: The mind is like a parachute; it works best when it is open.


                  Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Colin Angus Mackay wrote:

                  The mind is like a parachute; it works best when it is open.

                  I don't know about that. I've seen some minds that are so far open that air just passes straight through them!

                  Dave Kreskowiak Microsoft MVP - Visual Basic

                  D 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Colin Angus Mackay wrote:

                    The mind is like a parachute; it works best when it is open.

                    I don't know about that. I've seen some minds that are so far open that air just passes straight through them!

                    Dave Kreskowiak Microsoft MVP - Visual Basic

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #9

                    Dave Kreskowiak wrote:

                    Colin Angus Mackay wrote: The mind is like a parachute; it works best when it is open. I don't know about that. I've seen some minds that are so far open that air just passes straight through them!

                    Don't be so open minded your brain falls out.

                    -- Rules of thumb should not be taken for the whole hand.

                    D 1 Reply Last reply
                    0
                    • D Dan Neely

                      Dave Kreskowiak wrote:

                      Colin Angus Mackay wrote: The mind is like a parachute; it works best when it is open. I don't know about that. I've seen some minds that are so far open that air just passes straight through them!

                      Don't be so open minded your brain falls out.

                      -- Rules of thumb should not be taken for the whole hand.

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      dan neely wrote:

                      Don't be so open minded your brain falls out.

                      :laugh: It's way, way, way too late for most of those people! Their brains have long since been washed down the storm drains...

                      Dave Kreskowiak Microsoft MVP - Visual Basic

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups