468,733 Members | 1,627 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

Freeing Memory

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 what i assume is
the last reference to them) and I want them to be GCed. But for some
reason my app is leaking memory and that is the only place I can think
of that something could be going wrong. I want to just say
object.GetRidOfThisStupidObject() or GetRidOfThisStupidObject(object)
and have it do its thing, regardless of weather or not there are other
references to it (not that I can imagine where they are). Unfortunatly
my app is a service too so memory leaks just wont do.

Thanks,
Josh Powers

Nov 17 '05 #1
15 1558
Josh,

There is no way to do this. If you were able to, it would make
programming next to impossible. Imagine a system where references could be
set to null at any time. It would be a nightmare.

What gives you the impression you are leaking memory? If it is the task
manager, then that's not an accurate number to draw this kind of conclusion
on.

Also, a GC will occur when there is pressure to do so. If your
references are released, then the object will be collected.

Does the object have any references to COM objects or to unmanaged
handles? If so, you should be implementing the IDisposable interface on it,
and calling Dispose when you are done with the class.

In the end, you are going to have to do some profiling and/or debugging
to figure out what is really going on.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"yehoshua" <ba****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 what i assume is
the last reference to them) and I want them to be GCed. But for some
reason my app is leaking memory and that is the only place I can think
of that something could be going wrong. I want to just say
object.GetRidOfThisStupidObject() or GetRidOfThisStupidObject(object)
and have it do its thing, regardless of weather or not there are other
references to it (not that I can imagine where they are). Unfortunatly
my app is a service too so memory leaks just wont do.

Thanks,
Josh Powers

Nov 17 '05 #2
"yehoshua" <ba****@yahoo.com> wrote:
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 what i assume is the last
reference to them) and I want them to be GCed.
If there are no references, then no variables can still refer to the
object. In that case, you no longer have a "name" for the object, so
you couldn't pass it to any method as a parameter anyway!
But for some reason my app is leaking memory and that
is the only place I can think of that something could be
going wrong.


How do you know that you are leaking memory?

P.
Nov 17 '05 #3
It's not a memory leak, if your object is no longer rooted it will get
collected (non deterministically), freeing objects by calling GC.Collect()
is something you should never do. All it does is disturbing the process of
GC and finalization, it slows down your application and you get nothing
back. If you can't accept this you should stay away from a managed
environment like .NET.
Willy.

"yehoshua" <ba****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 what i assume is
the last reference to them) and I want them to be GCed. But for some
reason my app is leaking memory and that is the only place I can think
of that something could be going wrong. I want to just say
object.GetRidOfThisStupidObject() or GetRidOfThisStupidObject(object)
and have it do its thing, regardless of weather or not there are other
references to it (not that I can imagine where they are). Unfortunatly
my app is a service too so memory leaks just wont do.

Thanks,
Josh Powers

Nov 17 '05 #4
Josh:

You can set the object reference to null to make tell the GC that this
is a prime candidate for collection:

myobj = null;

Then, tell the GC to do its work... I forget the exact call, but it's
something like:
GC.CollectRightNow();

People tend to do this when they allocate large, complex data
structures and want to free memory.

Also, leaks are often created when your C# object holds a handle to an
unmaganged resource - be careful that you are properly Dispose()-ing of
your C# objects and freeing all unmanaged handles.

Best,
John

Nov 17 '05 #5
Nicholas,

I am under the impresion I am leaking memory because as my application
runs the PF Usage in the task manager continues to grow (it doesnt do
this while my application is not running) and as soon as I kill the
talk the usage drops back to a normal level. If I dont end my task
then after about a day or two the system crashes. No I dont have any
references to unmanaged code. I am using WMI in my object but I dont
think that should be a problem.

I am cant figure out how I could have any extra references to the
objects in my code because I am only storing the objects in 1 of 4
application level arrays and they get moved between the arrays until
they are eventually removed from the last array when the information in
them is written to the Database. It strikes me that as soon as I
remove the object from the list and the temporary reference i am using
in my function goes out of scope the object should be eligable for GC.
I have even tried calling GC.Collect() to ensure that any objects that
should be cleaned up are cleaned up and still my program grows until it
crashes the system.

I have tried debuging but I cant seem to find any place that objects
are being created and references are being kept without my knowledge.

Thanks,
Josh Powers

PS I happen to like c++ which is a system in which references can be
set to null at any time because If I am done with it i should be done
with it.

Nov 17 '05 #6
In that day or two, what is the cause of the crash? Is it an exception
that comes out of your application?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"yehoshua" <ba****@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Nicholas,

I am under the impresion I am leaking memory because as my application
runs the PF Usage in the task manager continues to grow (it doesnt do
this while my application is not running) and as soon as I kill the
talk the usage drops back to a normal level. If I dont end my task
then after about a day or two the system crashes. No I dont have any
references to unmanaged code. I am using WMI in my object but I dont
think that should be a problem.

I am cant figure out how I could have any extra references to the
objects in my code because I am only storing the objects in 1 of 4
application level arrays and they get moved between the arrays until
they are eventually removed from the last array when the information in
them is written to the Database. It strikes me that as soon as I
remove the object from the list and the temporary reference i am using
in my function goes out of scope the object should be eligable for GC.
I have even tried calling GC.Collect() to ensure that any objects that
should be cleaned up are cleaned up and still my program grows until it
crashes the system.

I have tried debuging but I cant seem to find any place that objects
are being created and references are being kept without my knowledge.

Thanks,
Josh Powers

PS I happen to like c++ which is a system in which references can be
set to null at any time because If I am done with it i should be done
with it.

Nov 17 '05 #7
Unfortunatly I havent actually caught it crashing becuase it keeps
happening at night when the computer is locked and when I get back in
the morning It wont even let me unlock it to see what is displayed. I
just added try catch blocks to everything and started outputing the
errors to a text file as they happen so I may get a little more
information out of that.

Nov 17 '05 #8
John Puopolo <jp******@mvisiontechnology.com> wrote:
You can set the object reference to null to make tell the GC that this
is a prime candidate for collection:

myobj = null;
No, that doesn't tell the GC that it's a candidate for collection. The
next time the GC runs, it just won't use myobj as a root, that's all.
If myobj is a local variable, setting it to null won't do anything
useful unless the JIT couldn't work out that the variable isn't used
any more (which is very occasionally the case). I rarely find it useful
to set member variables to null - usually if classes are designed
properly, they could potentially use all their members until the owning
objects themselves are ready to be garbage collected.
Then, tell the GC to do its work... I forget the exact call, but it's
something like:
GC.CollectRightNow();


And you should very, very rarely call that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
I have a small idea , I think that it s helpfull ..
just try to minimize the application when it's working , I mean when the
application is minimized it takes a small part from the memory , try that
using the task manager ,
please if it works tell me

Nov 17 '05 #10
Jon:

Good point - the null ref eliminates the object as root for the GC.

Outside of "good class design" and the dispose convention, there are
very few other ways to "really release stuff now."

All arguments of .NET productivity aside (both human and machine
efficiency, which I subscribe to), as a C++ programmer for a very long
time, I can understand this poster's frustration with having less
control than he's used to.

Best,
John
Jon wrote:
John Puopolo <jp******@mvisiontechnology.com> wrote:
You can set the object reference to null to make tell the GC that this
is a prime candidate for collection:

myobj = null;


No, that doesn't tell the GC that it's a candidate for collection. The
next time the GC runs, it just won't use myobj as a root, that's all.
If myobj is a local variable, setting it to null won't do anything
useful unless the JIT couldn't work out that the variable isn't used
any more (which is very occasionally the case). I rarely find it useful
to set member variables to null - usually if classes are designed
properly, they could potentially use all their members until the owning
objects themselves are ready to be garbage collected.
Then, tell the GC to do its work... I forget the exact call, but it's
something like:
GC.CollectRightNow();


And you should very, very rarely call that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 17 '05 #11
John Puopolo <jp******@mvisiontechnology.com> wrote:
Good point - the null ref eliminates the object as root for the GC.
Not quite - it eliminates that *variable*. There's a big difference.
(And just not using the variable again in that method will usually
eliminate the variable as a root for the GC too (in release mode). The
JIT's clever like that :)
Outside of "good class design" and the dispose convention, there are
very few other ways to "really release stuff now."
Dispose doesn't release memory either, of course.
All arguments of .NET productivity aside (both human and machine
efficiency, which I subscribe to), as a C++ programmer for a very long
time, I can understand this poster's frustration with having less
control than he's used to.


I can understand the frustration to start with, but hopefully it'll
disappear as he takes on more of a "don't worry about it" attitude :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
Nicholas,

Okay I spent the last serveral days doing some serious exception
handling and memory profiling. As far as exceptions go I have found
that my program is throwing mostly "Out of Memory" exceptions in
ManagementException's (From WMI). The odd part is that is it throwing
it from very early in the program while the program is still running
fine. On top of that I have been using the .NET Memory Profiler to try
to track down my leak. Unfortunatly as far as the .net mp tells me I
dont have and classes that arnt getting gced when they should be. In
fact after running for several hours (while the PF Meter steadly
climes) .net mp says my memory is almost exactly the same size (just a
couple of bytes off) once the application is back in the same state. I
dont get it. Anyways I am begging to think that WMI isnt intended to
be used on quite the level I am using it. I have 2 WMI events
registered on around 50 comptuters and once one of the events fires I
stop it and do a query every 30 seconds to that computer. So does
anyone have any ideas? Anyone know if WMI just cant handle that load?
Anything?

Nov 17 '05 #13
Seems like you are leaking unmanaged memory because you aren't disposing
your resources when done with it, check your memory counters using perfmon,
important are the process - private bytes and the CLR perf counters like
surviving finalizers.

I'm not clear what you mean by this ... "Out of Memory" exceptions in
ManagementException's (From WMI). mind to be a more explicit

Willy.
"yehoshua" <ba****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Nicholas,

Okay I spent the last serveral days doing some serious exception
handling and memory profiling. As far as exceptions go I have found
that my program is throwing mostly "Out of Memory" exceptions in
ManagementException's (From WMI). The odd part is that is it throwing
it from very early in the program while the program is still running
fine. On top of that I have been using the .NET Memory Profiler to try
to track down my leak. Unfortunatly as far as the .net mp tells me I
dont have and classes that arnt getting gced when they should be. In
fact after running for several hours (while the PF Meter steadly
climes) .net mp says my memory is almost exactly the same size (just a
couple of bytes off) once the application is back in the same state. I
dont get it. Anyways I am begging to think that WMI isnt intended to
be used on quite the level I am using it. I have 2 WMI events
registered on around 50 comptuters and once one of the events fires I
stop it and do a query every 30 seconds to that computer. So does
anyone have any ideas? Anyone know if WMI just cant handle that load?
Anything?

Nov 17 '05 #14
(I apologize for jumping in the middle of this but I've just run into a
similar situation).

In every example I've seen for implementing IDisposable, there's always
a section in the Dispose method where it says "dispose of unmanaged
resources here" but it never seems to provide an example.

I've got a C# class that has a reference to an application via COM. No
matter what method I call on the object, it never gets garbage collected
(i.e. when I quit my C# program, the other program is still running in
the task manager even though no one has a reference to it anymore ...
and yes, I'm calling the Quit() method on the COM object ... no luck.)

I can write code to terminate all running instances of the other
application but that's obviously a problem if there are other instances
besides the one I started.

Any thoughts?

Thanks,

Andy

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #15
I'm not sure what application you are talking about and in what context this
application is being used, but I assume it's Excel you are talking about.
Now, Excel is an out-proc COM automation server hosted in a Win32 process.
The fact that you release all COM references to the automation object or
objects wont make the application (the process) go away, that's the task of
Quit(), but calling Quit() doesn't guarantee the process will terminate
either, it's possible that dialog pops-up requesting a user action or an
error occurred during exit processing, in this case the process won't
terminate before a user takes an action. And this is the point where thing
might go wrong when using Excel in the context of a non interactive user. So
my question is - what kind of COM client is automating this COM server?

Willy.
"Andy Haines" <ah*****@minitab.com> wrote in message
news:eF**************@TK2MSFTNGP15.phx.gbl...
(I apologize for jumping in the middle of this but I've just run into a
similar situation).

In every example I've seen for implementing IDisposable, there's always
a section in the Dispose method where it says "dispose of unmanaged
resources here" but it never seems to provide an example.

I've got a C# class that has a reference to an application via COM. No
matter what method I call on the object, it never gets garbage collected
(i.e. when I quit my C# program, the other program is still running in
the task manager even though no one has a reference to it anymore ...
and yes, I'm calling the Quit() method on the COM object ... no luck.)

I can write code to terminate all running instances of the other
application but that's obviously a problem if there are other instances
besides the one I started.

Any thoughts?

Thanks,

Andy

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by disco | last post: by
12 posts views Thread by f.oppedisano | last post: by
11 posts views Thread by Rodrigo Dominguez | last post: by
6 posts views Thread by Fernando Cacciola | last post: by
4 posts views Thread by Atul Sureka | last post: by
5 posts views Thread by Amogh | last post: by
66 posts views Thread by karthikbalaguru | last post: by
9 posts views Thread by david | last post: by
11 posts views Thread by vivek | last post: by
25 posts views Thread by Andreas Eibach | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.