Re: memory leak or not?
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sat, 4 Nov 2006 07:55:16 -0000
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
.
- Follow-Ups:
- Re: memory leak or not?
- From: Cor Ligthert [MVP]
- Re: memory leak or not?
- References:
- Re: memory leak or not?
- From: Marina Levit [MVP]
- Re: memory leak or not?
- From: Jon Skeet [C# MVP]
- Re: memory leak or not?
- From: Cor Ligthert [MVP]
- Re: memory leak or not?
- From: Jon Skeet [C# MVP]
- Re: memory leak or not?
- From: Cor Ligthert [MVP]
- Re: memory leak or not?
- From: Jon Skeet [C# MVP]
- Re: memory leak or not?
- From: Cor Ligthert [MVP]
- Re: memory leak or not?
- From: Jon Skeet [C# MVP]
- Re: memory leak or not?
- From: Cor Ligthert [MVP]
- Re: memory leak or not?
- From: Jon Skeet [C# MVP]
- Re: memory leak or not?
- From: Cor Ligthert [MVP]
- Re: memory leak or not?
- Prev by Date: Re: Memory Cleanup
- Next by Date: Re: Thread Message Loop in C#?
- Previous by thread: Re: memory leak or not?
- Next by thread: Re: memory leak or not?
- Index(es):
Relevant Pages
|