Calling destructor

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

Hello,

I just wonder how can i destroy an object?
C# doesn't have a 'delete' keyword.
I know about garbage collection, but my program generates a large graph of
objects, which will ocuppy a lot of memory space, and i want to be able to
clar the mess manually, i don't want to depend on garbage collection, as it
occures in unpredictable time.

Thank you,
Andrey
 
Manages Code per definition included implicit memory management (Garbage
Collection) and C# does not work with pointers directly (even though you
can in an unmanaged code section). Once an object does not have any active
references, then it will be a candiate for garbage collection and you will
not
know when the memory will be reclaimed.

However, it is possible to force garbage collection to occur, by calling the
GC.Collect() method. The GC.Collect() method also contains an overload
which enables you to specify which range in the 0-n generations should be
collected.

Hope this help,

//Andreas
 
So from what you are saying, there is no way to explicitly remove an object
in managed code?
Hm.. i definitely remember i've read in a C# book that you can do that...
well, probably something's mixing up in my memory...

Andrey
 
Besides from forcing garbage collection, I do not know of any
other ways. This is from the .NET Framework documentation:

"The .NET Framework's garbage collector manages the allocation
and release of memory for your application. Each time you use the
new operator to create an object, the runtime allocates memory for
the object from the managed heap. As long as address space is
available in the managed heap, the runtime continues to allocate
space for new objects. However, memory is not infinite. Eventually
the garbage collector must perform a collection in order to free
some memory. The garbage collector's optimizing engine determines
the best time to perform a collection, based upon the allocations
being made. When the garbage collector performs a collection, it
checks for objects in the managed heap that are no longer being
used by the application and performs the necessary operations to
reclaim their memory."

//Andreas
 
MuZZy said:
So from what you are saying, there is no way to explicitly remove an object
in managed code?
Hm.. i definitely remember i've read in a C# book that you can do that...
well, probably something's mixing up in my memory...


Hi Andrey,

Something you can do, that will help, is to set references to null for those
objects you are no longer using. That way, you give the GC a chance to
clean them up for you. As long as you hold references to an object, the GC
can't clean them up.

Joe
 
Joe Mayo said:
Something you can do, that will help, is to set references to null for those
objects you are no longer using. That way, you give the GC a chance to
clean them up for you. As long as you hold references to an object, the GC
can't clean them up.

It's rarely a good idea to do so, however. Most of the time, I find
that any time I know a variable is no longer needed, either it's a
local variable and the JIT would be able to work out that it's no
longer needed, or the enclosing object is *also* no longer needed, so
the variable isn't preventing garbage collection anyway.

I can't remember the last time I found it a good idea to set a variable
to null explicitly for the purpose of garbage collection.
 
Jon Skeet said:
It's rarely a good idea to do so, however. Most of the time, I find
that any time I know a variable is no longer needed, either it's a
local variable and the JIT would be able to work out that it's no
longer needed, or the enclosing object is *also* no longer needed, so
the variable isn't preventing garbage collection anyway.

I can't remember the last time I found it a good idea to set a variable
to null explicitly for the purpose of garbage collection.


OP described how he had a large graph of objects and needed the ability to
clear <something> manually. Because <something> is unclear and the specific
data structure implementation and architecture is unknown, making
assumptions about proper design may not be accurate.

For example, what if the graph data structure persists for the life of the
application, yet he needs the ability to add/remove nodes on a continuous
basis. In such a scenario, setting a reference to the object to null
effectively removes it from the graph and makes it available for GC
(providing no other references exist).

In another setting, if he wanted to release the entire graph or the object
containing the graph, then the scenario you describe could be more
appropriate.

I'm not particularly inclined to dismiss someone else's design or ideas just
because I haven't seen it before.

Joe
 
Joe Mayo said:
OP described how he had a large graph of objects and needed the ability to
clear <something> manually. Because <something> is unclear and the specific
data structure implementation and architecture is unknown, making
assumptions about proper design may not be accurate.
True.

For example, what if the graph data structure persists for the life of the
application, yet he needs the ability to add/remove nodes on a continuous
basis. In such a scenario, setting a reference to the object to null
effectively removes it from the graph and makes it available for GC
(providing no other references exist).

Yes - although that would usually be part of a "collection type"
operation. While it's certainly possible that this is the case, I think
it's unlikely.
In another setting, if he wanted to release the entire graph or the object
containing the graph, then the scenario you describe could be more
appropriate.

I'm not particularly inclined to dismiss someone else's design or ideas just
because I haven't seen it before.

Fair enough - but neither am I inclined to encourage poor coding in
terms of writing "something=null;" all over the place when it's not
needed, which I've unfortunately seen too often :(
 
1. Use the using statement as below

using (Form1 f1=new Form1())
{
f1.Show();
}

As soon as the Show method is called the form will disappear because the
scope of the object is only within the using block.

2. You can call the Dispose method of the GC class.

GC.Dispose();

Although, you are explicitly invoking the GC, the collection is not
forced immediately.



with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Ravichandran J.V. said:
1. Use the using statement as below

using (Form1 f1=new Form1())
{
f1.Show();
}

As soon as the Show method is called the form will disappear because the
scope of the object is only within the using block.

Not quite - it'll disappear because the using block means that Dispose
is automatically called on the form. If you remove the "using" block,
just using a normal block so that the variable still goes out of scope
but Dispose isn't called, the form will stay up until the garbage
collector next happens to run and decide to finalize the form.
2. You can call the Dispose method of the GC class.

GC.Dispose();

Although, you are explicitly invoking the GC, the collection is not
forced immediately.

Slight correction: you mean GC.Collect here.
 
Quite right, Jon, I meant the Collect method. Thanks for the correction.

Let me add on to something to what you have stated as a response to my
response.

"Not quite - it'll disappear because the using block means that Dispose
is automatically called on the form."

Dispose is not automatically called. It is called because the Form class
implements IDisposable. I am sure you knew that. The using block scope
does not automatically call Dispose on user-defined classes if the
classes do not implement/override the Dispose method in them.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Use the using block for determinstic collection because the objects
scoped within the using block are destroyed automatically by the GC as
soon as the using block ends.

using (Form1 f=new Form1())
{

}

f.Show(); // Error. Object 'f' does not exist.

The 'f' object will be destroyed at the end of the using block.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Ravichandran J.V. said:
Quite right, Jon, I meant the Collect method. Thanks for the correction.

Let me add on to something to what you have stated as a response to my
response.

"Not quite - it'll disappear because the using block means that Dispose
is automatically called on the form."

Dispose is not automatically called.

Yes it is, by the definition of the using block.
It is called because the Form class
implements IDisposable. I am sure you knew that. The using block scope
does not automatically call Dispose on user-defined classes if the
classes do not implement/override the Dispose method in them.

The using block doesn't *allow* you to use it if the resource declared
doesn't implement IDisposable. For instance:

using (String x = "hello")
{
}

generates a compilation error: "Cannot implicitly convert type
'string' to 'System.IDisposable'"
 
Ravichandran J.V. said:
Use the using block for determinstic collection because the objects
scoped within the using block are destroyed automatically by the GC as
soon as the using block ends.

No they're not. Dispose is called on them, but that's not the same as
them being garbage collected.
using (Form1 f=new Form1())
{

}

f.Show(); // Error. Object 'f' does not exist.

The 'f' object will be destroyed at the end of the using block.

f is not an object, it's a variable. The variable is out of scope, but
that doesn't mean that the object is destroyed. It will have been
*disposed*, but that's a different matter.
 
Back
Top