Re: memory leak or not?



Cor Ligthert [MVP] <notmyfirstname@xxxxxxxxx> wrote:
I did not discuss that and don't have in any way an other opinion. Why are
you forever proving things that I don't deny.

Well, you said:

<quote>
As it had been a "normal" reference type the object would in my idea
not be garbaged.
</quote>

I've shown that a "normal" reference type *can* be garbage collected.
If that's not what you meant, you should be clearer about what you're
talking about.

I only say that a string is an in the method created object, while non
immutable objects are just references, if they ain't created new.

Where do you get this strange idea that strings behave differently
though? String literals are slightly different, in that they're kept in
a pool somewhere so won't be garbage collected until the application
domain is unloaded, but no-one's mentioned literals in this thread.

I still don't know what you mean by "an in the method created object" -
but strings definitely behave like other objects. If I call:

string x = new string ('y', 10);

that's created a new object which can be garbage collected like any
other. The value of x is just a reference in exactly the same way as it
would be in:

object x = new object();

Perhaps it's best to make this concrete, and go back to my original
example. I'll fill in GrabSomeLongString. How many objects do you
believe are created in the following code:



void Foo()
{
string x = GrabSomeLongString();
Console.WriteLine (x.Length);

// Lots more code here

// Nothing referring to x

// End of method
}


string GrabSomeLongString()
{
return new string ('z', 1000);
}


I believe there's *one* object created, a string, which the variable x
refers to. After Console.WriteLine (x.Length), that string variable is
eligible for garbage collection.

So, how many objects do you think are created, and when do you believe
they're eligible for GC?

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



Relevant Pages

  • Re: Complex Specified Information - Pitman Formula
    ... Therefore a significant match between a reference and a test ... string is good evidence of non-random production. ... and there are no finite algorithms to compute their digits. ... probabilities of the different symbols the information source can produce. ...
    (talk.origins)
  • Re: String Reference Type
    ... All unary and binary operators have predefined implementations that are ... Therefore its always allocated in the heap and a variable of string ... As with all classes in this case y and x both reference the same String ... language depandant matter as below. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Abstract class variables question
    ... But as I think you've seen elsewhere in this thread, a value type can exist inside a class and in that case the value type is stored in the heap with the rest of the class instance. ... But as far as the "faster" goes, yes...to some extent value types have less overhead than reference types, and so can perform better in certain cases. ... Well, that would be true for a string object too, if there was any way to actually change a string. ... Seriously though, it is practically always the case that when you are writing an assignment to a reference, you're replacing the reference held by the variable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Measurement of pitch
    ... as the method used by the Pythagoreans. ... of these reference units in the quantity to be measured. ... vibrating string seems as good as anything. ... The string or pendulum in question could no doubt be specified exactly, ...
    (sci.physics)
  • Re: Abstract class variables question
    ... I think I understand boxing a little better now. ... the object that is on the heap. ... value types are copied to the heap and made into an object and reference ... String types are already reference types and all we are doing when we do ...
    (microsoft.public.dotnet.languages.csharp)