473,503 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 36507
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
3031
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
2259
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
2746
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
1750
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
1670
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
2248
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
3635
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
11
1976
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
4679
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
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.