none
When will an instance collected by GC? RRS feed

  • Question

  • As far as I see, GC will automatically collect the instance that is useless at some time (random). So I feel very curious about that——

    1)If I assign "null" to an instance, will this mean the instance be collected by GC?

    2)If I use "using……" statement, Dispose only "dispose" the inner contents of the instances, so the instance won't be collected, am I right?Or will it be collected by GC immediately?Why?I know that Dispose has nothing to do with GC, so even if u call Dispose manually this instance won't be immediately collected by GC until it's "useless"?

    3)What's "useless"?How does GC say some object is "useless"?null value?Any other situations?Is it related to do with accessor modifier and the life range of a variable?

    Plz quote each of my question and reply step by step……

    Many thanks!

    • Changed type TimoYang Wednesday, July 18, 2012 1:08 AM This is a question
    Wednesday, July 18, 2012 1:08 AM

Answers

  • In order to understand the following responses, you need to understand something:  Variables in .net are pointers.  When you set a variable to be null, you are merely redirecting the pointer to null, meaning the pointer no longer points.

    So...

    1.

    You don't really assing null to an instance, you assign null to a pointer pointing to an instance.  Will this alone trigger garbage collection?  No.  All other pointers to the instance must also be nulled out and then GC needs to run.  It won't necessarily run as soon as an object is "orphaned" by all its pointers.

    2.

    The using statement calls Dispose.  Dispose makes sure consumed system resources are freed, but the object itself remains in memory and it won't be collected unless all pointers pointing to it are nullified or pointed to another object.  And the same rule applies:  GC may or may not start as soon as this happens.

    3.

    What you call useless is merely an orphaned instance (not really the proper term) of an object.  An orphan is useless, yes, because it cannot be accessed by code and therefore becomes a waste of RAM.  Note that what is useless is the object, not the pointer formerly pointing to the object.


    Jose R. MCP
    Code Samples

    • Proposed as answer by Mike FengModerator Friday, July 27, 2012 9:48 AM
    • Marked as answer by TimoYang Saturday, July 28, 2012 3:22 AM
    Wednesday, July 18, 2012 4:04 AM
  • Yes, an "orphan" object is an object that is not accessible because there are no pointers pointing to it.  Only the garbage collector knows about it for the sole purpose of destroying it.

    Jose R. MCP
    Code Samples

    • Marked as answer by TimoYang Saturday, July 28, 2012 3:23 AM
    Wednesday, July 18, 2012 6:01 AM

All replies

  • In order to understand the following responses, you need to understand something:  Variables in .net are pointers.  When you set a variable to be null, you are merely redirecting the pointer to null, meaning the pointer no longer points.

    So...

    1.

    You don't really assing null to an instance, you assign null to a pointer pointing to an instance.  Will this alone trigger garbage collection?  No.  All other pointers to the instance must also be nulled out and then GC needs to run.  It won't necessarily run as soon as an object is "orphaned" by all its pointers.

    2.

    The using statement calls Dispose.  Dispose makes sure consumed system resources are freed, but the object itself remains in memory and it won't be collected unless all pointers pointing to it are nullified or pointed to another object.  And the same rule applies:  GC may or may not start as soon as this happens.

    3.

    What you call useless is merely an orphaned instance (not really the proper term) of an object.  An orphan is useless, yes, because it cannot be accessed by code and therefore becomes a waste of RAM.  Note that what is useless is the object, not the pointer formerly pointing to the object.


    Jose R. MCP
    Code Samples

    • Proposed as answer by Mike FengModerator Friday, July 27, 2012 9:48 AM
    • Marked as answer by TimoYang Saturday, July 28, 2012 3:22 AM
    Wednesday, July 18, 2012 4:04 AM
  • Ignoredrei,

                 A good link for you.


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, July 18, 2012 4:55 AM
  • Hi webJose:-)

    Do you mean "orphan" is —— an instance that isn't used or referred by any other points?!

    Wednesday, July 18, 2012 5:00 AM
  • Ignoredrei,

                 A good link for you.


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Many thanks first!

    Then What's Generation 2 in the image?What's "emphemeral segment"?I don't know the relationship with them at all……:(

    Plz offer me a detailled explaination, it's really hard to understand……

    Wednesday, July 18, 2012 5:08 AM
  • Hope this answers you.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, July 18, 2012 5:56 AM
  • Yes, an "orphan" object is an object that is not accessible because there are no pointers pointing to it.  Only the garbage collector knows about it for the sole purpose of destroying it.

    Jose R. MCP
    Code Samples

    • Marked as answer by TimoYang Saturday, July 28, 2012 3:23 AM
    Wednesday, July 18, 2012 6:01 AM