473,395 Members | 1,823 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,395 software developers and data experts.

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 1743
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
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...
4
by: Atul Sureka | last post by:
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...
5
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
9
by: david | last post by:
I will past only two segments from the code it should be enough to see what I did wrong, I think I know there I made a mistake, but how to fix it I can not tell. This why I need help from you all....
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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
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,...

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.