473,287 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Freeing memory in C#

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

Mar 31 '06 #1
4 36478
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

"Atul Sureka" <at*********@persistent.co.in> a écrit dans le message de
news: e0**************@TK2MSFTNGP12.phx.gbl...
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

Mar 31 '06 #2
"Atul Sureka" <at*********@persistent.co.in> a écrit dans le message de
news: e0**************@TK2MSFTNGP12.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 31 '06 #3


Joanna Carter [TeamB] wrote:
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 :)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Mar 31 '06 #4

"Atul Sureka" <at*********@persistent.co.in> wrote in message
news:e0**************@TK2MSFTNGP12.phx.gbl...
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++.
Mar 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: f.oppedisano | last post by:
Hi, i would like to allocate two structures making only one malloc call. So i do prt=malloc(sizeof(struct1)+sizeof(struct2)); After this operation i make two pointers one to the first struct...
11
by: Rodrigo Dominguez | last post by:
there are sometimes that I use third party libraries, I use some functions that returns char * or structs, etc. sometimes the memory that is returned by those libraries, when I try to free this...
6
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT...
15
by: yehoshua | last post by:
Is there anyway to explicitly tell the garbage collector you are done with an object? In my app I have a point where I am sure there should be no references to an set of objects (well i remove...
2
by: tristan.chaplin | last post by:
I have an unmanaged dll that returns a pointer (that it allocates, I didn't allocate it with Marshal.AllocHGlobal), how do I free the memory when I'm finished with it? It doesn't seem to like me...
2
by: Chris | last post by:
I have a Bayesian simulation package that keeps running into memory allocation problems. I have a feeling this has something to do with Python (2.5.1.1) not freeing memory. The program essentially...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
11
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it...
25
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.