-2

I have some objects which have public events that objects 'listen' to to get messages pass through the system. The problem is my programs memory footprint keeps growing and I am wondering whether the GC is failing to collect the objects because other objects are subscribed to it's events

call.requestingHangup+=new CallEventHandler (Call_requestingHangup);

now if nothing else a named reference to this 'Call' however we are still subscribed to its event will the GC remove it. Also are there any memory diagnositic tools for c# like valgrind for native code?

Thank You

H H
  • 263,252
  • 30
  • 330
  • 514
111111
  • 15,686
  • 6
  • 47
  • 62
  • If something is using an object, it will not be subject to GC. – ChrisBint Jul 04 '11 at 14:05
  • For memory diagnostic tools, you can try the free CLR Profiler from MS (http://msdn.microsoft.com/en-us/library/ff650691.aspx) or JetBrains dotTrace which is a paid application (http://www.jetbrains.com/profiler/). – hdougie Jul 04 '11 at 14:06
  • @ChrisBint It isn't using it, it is just listening to the event. In the above example Call will fall out of scope eventually. My question i guess if handlers are not explicitly will the object persist. Thanks – 111111 Jul 04 '11 at 14:07
  • No, your `call` object is holding a reference, not being referenced. So it is not kept from being collected. – H H Jul 04 '11 at 14:09
  • 1
    possible duplicate of [Do event handlers stop garbage collection from occuring?](http://stackoverflow.com/questions/298261/do-event-handlers-stop-garbage-collection-from-occuring) – H H Jul 04 '11 at 14:10
  • Possible duplicate of http://stackoverflow.com/questions/298261/do-event-handlers-stop-garbage-collection-from-occuring – Tipx Jul 04 '11 at 14:13
  • Henk: that was my thought it seems odd to me but the program keeps growing in size whilst have only a small range of active objects. – 111111 Jul 04 '11 at 14:19
  • @1111: There are other places to get it wrong. But a growing footprint is not always a problem, so _how_ do you measure memory? I don't think you have a leak. – H H Jul 04 '11 at 14:24

1 Answers1

0

You should also de hook your events like below, when you are finished with them. This will make sure that they are GCed.

call.requestingHangup-=new CallEventHandler (Call_requestingHangup);

Please see other stackoverflow post which explains this better.

Community
  • 1
  • 1
Jethro
  • 5,896
  • 3
  • 23
  • 24
  • No, this is not necessary in this case. It is the right code for un-hooking though, although a bit long. `-=Call_requestingHangup` is enough. – H H Jul 04 '11 at 14:12
  • @Henk Holterman, it may not be necessary but is a good practice to get into. Please check http://stackoverflow.com/questions/380819/common-programming-mistakes-for-net-developers-to-avoid link, and comment Scott Langham made on "Common programming mistakes for .NET developers to avoid?" – Jethro Jul 04 '11 at 14:19
  • 1
    The necessity depends entirely on the circumstances. We can't tell if that's the case in this question. But I strongly reject unsubscribing events everywhere. – H H Jul 04 '11 at 14:22
  • @Henk Holterman, would it not be better to unsubscribe events when you are finished with them? Why do you strongly reject it? – Jethro Jul 04 '11 at 16:47
  • It leads to the same cluttering as `myLocalRef = null` everywhere. It is rarely necessary and should be used only when it is. – H H Jul 04 '11 at 17:20
  • But myLocalRef gets collected by the GC, does the GC know to do the same with the event? Would it not be better to -= (un hook) the event when you are done? – Jethro Jul 04 '11 at 17:27