Re: How to destroy arrays

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 06/01/04


Date: Tue, 1 Jun 2004 11:56:58 +0100

Cor Ligthert <notfirstname@planet.nl> wrote:
> > Just to clarify my meaning, I was asking when you believe the array
> > which the Foo variable's value was a reference to would be eligible for
> > garbage collection.
>
> I thought you did know that?

I know when it's eligible for garbage collection. What I was asking was
when *you* believed it would be eligible for garbage collection, and
what you'd have thought just from Scott's article.
 
> > Because when a is an integer, the garbage collector isn't interested in
> > whether or not the variable is reachable. When a is an array reference,
> > it is.
>
> Did you not know that as well?

Yes, I knew that before. It wasn't clear that you did, however.

> > So, in my example, because Foo is not used after
> > Console.WriteLine(Foo(0))
> > it isn't considered a "root" for garbage collection (in release mode -
> > in debug mode this optimisation isn't performed as the developer may
> > want to look at its value.
>
> Why should it not be, I thought I showed you that enough I hope I made it
> clear now for you?

Um, you haven't shed any light on garbage collection in this thread, as
far as I can see. What exactly do you think you've made clear, and
where?
 
> >> So why would that be different with = nothing.
>
> >Because when a is an integer, the garbage collector isn't interested in
> >whether or not the variable is reachable. When a is an array reference,
> >it is.
>
> Now I see you think that the = operater is for setting objects, no it is
> ment to set something, that can be 1 2 3 or nothing, depended on the kind of
> receiver it acts. And when a created object has no references anymore either
> way, do not make a mistake in that, (it can be also that it is referenced
> itself), than it is disposable (available) to the GC.

Not sure what you mean by "the kind of receiver it acts" but integers
themselves are never garbage collected. (Boxed versions are, of course,
but that's different.)

The = operator is for setting the value of a variable (or property),
either to a value type value or to a reference. Never to an actual
object. I think we agree on this, but I'm afraid I didn't understand
most of what you wrote above ("it can be also that it is referenced
itself" for instance)...

> > Actually, Scott didn't write that, fortunately - because it's not
> > true.Variables aren't "set to nothing automatically" - they're just not
> > considered to be roots by the garbage collector any more. What Scott
> > *did* imply (IMO) is that local variables were always considered to be
> > "roots" by the garbage collector until the end of the method. That's
> > not true.
>
> That was the reason of my first message to you, why are you additing to
> Scotts message. I nowhere readed that implyment of Scott it was only (YMO),
> however had told that, than I would not have connected a message.

Scott implied it when he specified the "rather than waiting for the end
of a procedure" in my opinion. If he'd said "rather than waiting for
the last use of the variable in the IL code" then I wouldn't have
written anything. He seemed to imply that there was something special
about the end of the procedure - otherwise why mention it?

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Java Generic programming using subclassing
    ... Java nowhere guarantees that the elements of an array ... Now chances are that any Java ... > reference will be visible through the other. ... > eligible for garbage collection, and will in fact be collected before ...
    (comp.lang.java.programmer)
  • Re: How do I Create ragged arrays in Excel VBA?
    ... garbage collection is based on reference counting. ... array B - a reference which won't be removed until *A* is ... When ReDim Bis run a brand new ...
    (microsoft.public.excel.programming)
  • Re: Performance issue with filestream.write on huge file
    ... converted to an XML element and outputed to a file. ... by the type array written which are not released by the GC. ... The caller, of course, still may have a reference and if so the bytewon't be eligible for garbage collection no matter what you do in that Writemethod. ...
    (microsoft.public.dotnet.framework)
  • Re: How to destroy arrays
    ... eligible for garbage collection in the Sub below? ... When a is an array reference, ... Actually, Scott didn't write that, fortunately - because it's not ... "roots" by the garbage collector until the end of the method. ...
    (microsoft.public.dotnet.general)
  • Re: [PHP] how to create a function and return two parameters????
    ... Ummm, look like I'll stick with number two. ... Scott F. ... don't return anything and pass both values by reference ... stick both values in an array and return the array ...
    (php.general)