Freeing memory in C#

A

Atul Sureka

Hi,

I want to free the object memory in C# - like we do using 'delete' keyword
in C++.
Lets say I have an object of some class and I want to explicitly free the
memory.
C# do not have any free or delete keyword to do so.

Also I don't want to wait for the GC to be called.

Is assigning any object to null means freeing the memory ??

Regards
Atul Sureka
 
S

Steph

hello,
you can use "Dispose"

like :
class toto : IsDisposable
{
// your code here
//
public void Dispose() {
//your destructor... set here all purge...
}
}

toto MyTest = new toto();

using(MyTest)
{
//your code ...
}
// and here toto.Dispose is automatically call ...

but (and i never test it) i think i good too, but not automatic :
toto MyTest = new toto();
//your code ... and
toto.Dispose(); // manual call the destructor...


"All under reserve !" ;-)

manu
 
J

Joanna Carter [TeamB]

"Atul Sureka" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| I want to free the object memory in C# - like we do using 'delete' keyword
| in C++.
| Lets say I have an object of some class and I want to explicitly free the
| memory.
| C# do not have any free or delete keyword to do so.

C# does not have deterministic finalization, simply null the reference and
the GC will do the work for you.

| Also I don't want to wait for the GC to be called.

Forcing the GC can be a lot less efficient than letting it do its own thing.

| Is assigning any object to null means freeing the memory ??

As I said, it signals the GC that the object is available for collection.

Joanna
 
H

Helge Jensen

Joanna said:
C# does not have deterministic finalization, simply null the reference and
the GC will do the work for you.

Nulling the reference won't really help much, try benchmarking it -- the
GC (atleast in .NET1+2 from M$) works pretty ok :)
 
N

Nick Hounsome

Atul Sureka said:
Hi,

I want to free the object memory in C# - like we do using 'delete' keyword
in C++.

I want to be a millionaire but it's not going to happen.
Lets say I have an object of some class and I want to explicitly free the
memory.
C# do not have any free or delete keyword to do so.

You can't do it because it is supposed to be idiot proof and idiots free
memory that is still referenced elsewhere hence there is no mechanism to
explicitly free memory.
Also I don't want to wait for the GC to be called.
Tough.

Is assigning any object to null means freeing the memory ??

No it means that it is available to be collected by the GC IF there are no
other references.

Basically - If you don't like the language why use it?

If freeing memory is that important to you but you still have to interact
with .NET then use mixed mode C++.
 

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