memory leak or not?

G

Guest

Does such a function result in a memory leak because of not explicitely
setting result = null?
------------------
public override string GetValueAsString()
{
StringBuilder result = new StringBuilder(10);
result.Append(Month.AsString());
result.Append('/');
result.Append(Day.AsString());
result.Append('/');
result.Append(Year.AsString());
return result.ToString();
}
------------------

Or does the compiler automatically gets that "result" goes off focus now?

best regards,
doc
 
M

Marina Levit [MVP]

No, when the variable is out of scope, it becomes eligible for garbage
collection.
 
G

Guest

Hi Marina,

Marina Levit said:
No, when the variable is out of scope, it becomes eligible for garbage
collection.

Thanks for confirmation. Moving from C++ where one has to take care of
everything makes me paranoid sometimes :)

doc
 
M

Martin Z

docschnipp said:
Hi Marina,



Thanks for confirmation. Moving from C++ where one has to take care of
everything makes me paranoid sometimes :)

doc

In that case, a warning: unless you use the "Using" block, you can't do
the neat tricks in C++ where destructors made objects clean up their
resources the moment they go out of focus. So on the one hand, GC
means memory leaking is much less likely, it also means that your
objects may retain resources (like file and DB connections) a lot
longer unless you manually free them - objects falling off the stack
don't clean up their resources until the GC runs, which may be a little
while.
 
J

Jeremy Cox

You omit to mention that this is for automatic calling of the Dispose()
method and not a dtor as such

To Marina - check whether the object in question implements IDisposable. If
so you should scope it within a using {} block. Dispose() will then get
automatically called when this block is left.
 
M

Marina Levit [MVP]

Sorry, why is the comment to me?

All I said was once it is out of scope, it becomes eligible to be garbage
collected. This is true regardless of whetehr or not it implements
IDisposable.
 
G

Guest

Martin Z said:
In that case, a warning: unless you use the "Using" block, you can't do
the neat tricks in C++ where destructors made objects clean up their
resources the moment they go out of focus. So on the one hand, GC
means memory leaking is much less likely, it also means that your
objects may retain resources (like file and DB connections) a lot
longer unless you manually free them - objects falling off the stack
don't clean up their resources until the GC runs, which may be a little
while.

Yes, I am aware of that fact. I started to do 2 things:

1) build my own "release" functions to gain control of other resources than
memory
2) despite the "going off scope" I am partially using an extra "objref =
null" to move the object on top of the GC. Especially when reusing a ref.

Doc
 
J

Jon Skeet [C# MVP]

Marina Levit said:
No, when the variable is out of scope, it becomes eligible for garbage
collection.

In this particular case it's true, but in a more general case it can be
earlier. For instance:

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

// Lots more code here

// Nothing referring to x

// End of method
}

The variable "x" does not prevent the long string from being garbage
collected (in release mode) before the end of the method, even though
it's still in scope.
 
K

Kevin Yu [MSFT]

Yes, I agree with Marina. After the using statement, it only calls the
Dispose method to release the unmanaged resources. In the case doc has
provided, everything is managed. So, all are the same. When the reference
to StringBuilder goes out of scope, it becomes garbage collectable.

There is no memory leak in this case.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
C

Cor Ligthert [MVP]

Jeremy,

It seems that a lot of people, like in my idea you, are thinking that
dispose is the same as the classic finalize method. I hope I don't disapoint
you: It is not. Marina's answer is completely correct whether Idispose is
implemented or not. Only if there are unmanaged resources used, then
Idisposable comes in scope.

Cor
 
C

Cor Ligthert [MVP]

"Reusing a ref" (in other words declaring it globaly) what is the sense of
that?
 
G

Guest

Cor Ligthert said:
"Reusing a ref" (in other words declaring it globaly) what is the sense of
that?

In this case it is a member of my class, like a reference to an object or a
collection.

I was asking myself if such a construct would help the compiler/runtime to
reuse the same memory block:

this.mysomething = null;
this.mysomething = new Something();

or if the null assignment is just waste of source code lines :)

I guess the latter.

cu
doc
 
W

Willy Denoyette [MVP]

| Jon,
|
| I am glad I can help you with this.
|
| The string is immutable what you are showing is not a reference.
|
| You can have a look at what immuatble means at these pages.
|
| http://www.yoda.arachsys.com/csharp/parameters.html
|
| :)



Not sure what you are talking about, what Jon is trying to say is that an
object can be GC'd even before it's reference leaves the scope.

Willy.
 
G

Guest

docschnipp said:
In this case it is a member of my class, like a reference to an object or a
collection.

I was asking myself if such a construct would help the compiler/runtime to
reuse the same memory block:

this.mysomething = null;
this.mysomething = new Something();

or if the null assignment is just waste of source code lines :)

I guess the latter.

cu
doc

If you expect the allocation to almost always cause a garbage collection
because it in turn creates an obscene amount of objects, then it makes
sense to dereference the previous object first. Otherwise it's just a
waste of code and time.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Of course it's a reference.

The fact that the object that the reference is referencing is immutable,
doesn't keep it from being a reference.

There is nothing magical about immutable objects, it only means that
they offer no methods or properties that will make it possible to change
their data.
Jon,

I am glad I can help you with this.

The string is immutable what you are showing is not a reference.

You can have a look at what immuatble means at these pages.

http://www.yoda.arachsys.com/csharp/parameters.html

:)

Cor
 
G

Guest

Göran Andersson said:
If you expect the allocation to almost always cause a garbage collection
because it in turn creates an obscene amount of objects, then it makes
sense to dereference the previous object first. Otherwise it's just a
waste of code and time.

Maybe someone with deeper knowledge of the implementation could elaborate
this a bit? It would be interesting to know.

doc
 
S

Sericinus hunter

Jon, will you please provide a reference where I can read about this?

Jon Skeet [C# MVP] wrote:
....
 
G

Guest

Sericinus hunter said:
What is 'x' then?

x ist just not existent anymore. it doesn't matter what it is. the memory
that x occupied has fallen back to the responsibility of the Garbage
Collection. Since your code does not access x anymore, it might it is already
gone. This is something the compiler does, it doesn't allocate a
register/memory/stack or whatever for it from this point on. But you can not
determine it. This is by design and actually very helpful, but has some
pitfalls.

I found a nice read here:
http://msdn.microsoft.com/library/d...ProgrammingEssentialsForGarbageCollection.asp

doc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top