new keyword scope

  • Thread starter Thread starter Neil B
  • Start date Start date
N

Neil B

In C++ when you create an object using the "new" keyword you usually use the
"delete" keyword to end it's scope. In C# I don't find the "delete" keyword.
How do you handle the scope of an object you create with "new"???

Thanks, Neil
 
Neil B said:
In C++ when you create an object using the "new" keyword you usually use
the
"delete" keyword to end it's scope. In C# I don't find the "delete"
keyword.
How do you handle the scope of an object you create with "new"???

Hi Neil,

C# has a garbage collector system, so you don't need to do explicit delete
as in C++.

If you have to free non-memory resources (like having a class that manages a
texture or file or socket...) then you can implement IDisposable interface
and the "dispose" pattern.

Giovanni
 
Neil B said:
In C++ when you create an object using the "new" keyword you usually use the
"delete" keyword to end it's scope. In C# I don't find the "delete" keyword.
How do you handle the scope of an object you create with "new"???

You don't. It will be garbage collected at some point after there are
no live reference to it.

(Note that "scope" in the language specification sense doesn't really
apply to the objects themselves - it's more to do with variables,
methods etc.)
 
That sounds great. No one wants to spend their time doing house work.

Question: If I have a program that's in a really big loop creating lots and
lots of new objects how does C# know when to garage collect them before I run
out of memory???? In other words, in general what provides the intiative and
the explicit knowledge that an object is no longer needed???

Thanks, Neil
 
Nothing does. When it detects memory pressume, the GC rebuilds a graph
from object to object (starting with top-level objects like threads
etc). Anything it can reach is still valid. Anything else is flagged
for removal.

You can try to force the hand of the GC, but it isn't recommended. It
knows what it is doing. For example, there is a generation system in
pace on the assumption that many objects are short lived. Objects are
created in gen-zero, where it is very efficient at reclaiming space.
Only if the object survives a GC (i.e. it is still in use at some
arbitrary time later) will it move out of gen-zero to gen-one, then
gen-two*. So most of the objects will be reclaimed with minimum fuss.

(*=for completeness there is also the "large object heap", where huge
objects [large arrays etc] live from inception; but that works
slightly differently)

Marc
 
Neil B said:
That sounds great. No one wants to spend their time doing house work.

Question: If I have a program that's in a really big loop creating lots
and
lots of new objects how does C# know when to garage collect them before I
run
out of memory???? In other words, in general what provides the intiative
and
the explicit knowledge that an object is no longer needed???

Every time you call new, the GC might decide it needs to run.
Thanks, Neil

And the word used wrt objects is "lifetime".
 
That sounds great. No one wants to spend their time doing house work.

Question: If I have a program that's in a really big loop creating lots and
lots of new objects how does C# know when to garage collect them before I run
out of memory???? In other words, in general what provides the intiative and
the explicit knowledge that an object is no longer needed???

Thanks, Neil

:





- Show quoted text -

Hi,

It does know, how the GC works is complex, but let's say that when an
object is not longer referenced it can be detected and marked as such,
so when the GC runs he knows which objects can be collected.

Take a look at the IDisposable interface and articles ni MSDN
 

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

Similar Threads


Back
Top