![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | memory managment Hello, When a control is disposed, should it first set its Tag property (if present) to null ? I use a commercial control and I saw (with a .NET memory profiler) that the objects I put in this property were not collected even when the control is disposed (until I set the tag property to null) -- Fred foleide@xxxxxx |
My System Specs![]() |
| | #2 (permalink) |
| | Re: memory managment Fred wrote: Quote: > Hello, > > When a control is disposed, should it first set its Tag property (if > present) to null ? > > I use a commercial control and I saw (with a .NET memory profiler) that > the objects I put in this property were not collected even when the > control is disposed (until I set the tag > property to null) > you can set the Tag property to null to make thae object that it references collectable. Otherwise there is rarely any reason to setting references to null. When the control gets collectable, any objects that it references (and isn't references from somewhere else) also automatically gets collectable. -- Göran Andersson _____ http://www.guffa.com |
My System Specs![]() |
| | #3 (permalink) |
| | Re: memory managment On Wed, 11 Jun 2008 07:32:30 +0200, "Fred" <foleide@xxxxxx> wrote: Quote: > Hello, > >When a control is disposed, should it first set its Tag property (if >present) to null ? If the control won't be eligible for garbage collection soon, then setting the Tag property to null will allow the object references by Tag to be collected sooner (assuming there are no other references to that object). Quote: >I use a commercial control and I saw (with a .NET memory profiler) that >the objects I put in this property were not collected even when the >control is disposed (until I set the tag >property to null) eligible for garbage collection when no live object has a reference it regardless of whether or not Dispose has been called. Dispose simply frees any resources that the object is holding. For UI controls Dispose releases the associated Windows control. In no case does Dispose cause the object to be garbage collected. If Dispose isn't called explicitly, it will be called when the object is garbage collected. Explicitly calling Dispose is simply a way to free resources in a deterministic way, since the object may not be garbage collected for a very long time. If you aren't holding any references to the control (it is removed from its container collection, any event handlers it added for other objects have been removed, you don't have any variables that hold reference to it, etc.) then it is eligible for garbage collection and so is the object that its Tab property references. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: memory managment "Jack Jackson" <jjackson@xxxxxx> a écrit dans le message de news:19l05498ecef7m1ul19q2q3scil57bhgm6@xxxxxx Quote: > On Wed, 11 Jun 2008 07:32:30 +0200, "Fred" <foleide@xxxxxx> > wrote: > Quote: >> Hello, >> >>When a control is disposed, should it first set its Tag property (if >>present) to null ? > If the control will soon be eligible for garbage collection, then no. > If the control won't be eligible for garbage collection soon, then > setting the Tag property to null will allow the object references by > Tag to be collected sooner (assuming there are no other references to > that object). > Quote: >>I use a commercial control and I saw (with a .NET memory profiler) >>that >>the objects I put in this property were not collected even when the >>control is disposed (until I set the tag >>property to null) > Calling Dispose has no effect on garbage collection. An object is > eligible for garbage collection when no live object has a reference it > regardless of whether or not Dispose has been called. Dispose simply > frees any resources that the object is holding. For UI controls > Dispose releases the associated Windows control. In no case does > Dispose cause the object to be garbage collected. If Dispose isn't > called explicitly, it will be called when the object is garbage > collected. Explicitly calling Dispose is simply a way to free > resources in a deterministic way, since the object may not be garbage > collected for a very long time. > > If you aren't holding any references to the control (it is removed > from its container collection, any event handlers it added for other > objects have been removed, you don't have any variables that hold > reference to it, etc.) then it is eligible for garbage collection and > so is the object that its Tab property references. Thank you Göran and Jack, I omitted to say that I was calling GC.Collect in my tests before to make a memory snapshot. I know that Dispose won't cause the object to be garbage collected immediatly. So : 1) Not settings the Tag property to null, I can see in the memory the disposed control and some of my classes instances (after GC.Collect). 2) Setting the Tag proprty to null, every thing disappear (after GC.Collect) According to your explainations, it means, I think, that there were somewhere some references to the control in my classes (I will have a look and correct this). A kind of circular reference that I broke setting the Tag property to null and permit the garbage collector to do its job ? So, about my question. Isn't it a best practice, if I develop a custom control, to set all it's possible extern references (as the Tag property) to null when disposing ? -- Fred foleide@xxxxxx |
My System Specs![]() |
| | #5 (permalink) |
| | Re: memory managment On Thu, 12 Jun 2008 13:15:03 +0200, "Fred" <foleide@xxxxxx> wrote: Quote: >"Jack Jackson" <jjackson@xxxxxx> a écrit dans le message de >news:19l05498ecef7m1ul19q2q3scil57bhgm6@xxxxxx Quote: >> On Wed, 11 Jun 2008 07:32:30 +0200, "Fred" <foleide@xxxxxx> >> wrote: >> Quote: >>> Hello, >>> >>>When a control is disposed, should it first set its Tag property (if >>>present) to null ? >> If the control will soon be eligible for garbage collection, then no. >> If the control won't be eligible for garbage collection soon, then >> setting the Tag property to null will allow the object references by >> Tag to be collected sooner (assuming there are no other references to >> that object). >> Quote: >>>I use a commercial control and I saw (with a .NET memory profiler) >>>that >>>the objects I put in this property were not collected even when the >>>control is disposed (until I set the tag >>>property to null) >> Calling Dispose has no effect on garbage collection. An object is >> eligible for garbage collection when no live object has a reference it >> regardless of whether or not Dispose has been called. Dispose simply >> frees any resources that the object is holding. For UI controls >> Dispose releases the associated Windows control. In no case does >> Dispose cause the object to be garbage collected. If Dispose isn't >> called explicitly, it will be called when the object is garbage >> collected. Explicitly calling Dispose is simply a way to free >> resources in a deterministic way, since the object may not be garbage >> collected for a very long time. >> >> If you aren't holding any references to the control (it is removed >> from its container collection, any event handlers it added for other >> objects have been removed, you don't have any variables that hold >> reference to it, etc.) then it is eligible for garbage collection and >> so is the object that its Tab property references. > >Thank you Göran and Jack, > >I omitted to say that I was calling GC.Collect in my tests before to >make a memory snapshot. I know that Dispose won't cause the object to be >garbage collected immediatly. > >So : >1) Not settings the Tag property to null, I can see in the memory the >disposed control and some of my classes instances (after GC.Collect). >2) Setting the Tag proprty to null, every thing disappear (after >GC.Collect) > >According to your explainations, it means, I think, that there were >somewhere some references to the control in my classes (I will have a >look and correct this). A kind of circular reference that I broke >setting the Tag property to null and permit the garbage collector to do >its job ? > >So, about my question. Isn't it a best practice, if I develop a custom >control, to set all it's possible extern references (as the Tag >property) to null when disposing ? collected, then yes that implies that something still references it. There are some tools that will give you information about what is keeping objects alive. It certainly won't hurt to null out references, but in general it should not be necessary. It might help to hide other problems, which could be either a good thing or a bad thing depending on your point of view. |
My System Specs![]() |
| | #6 (permalink) |
| | Re: memory managment Fred wrote: Quote: > I omitted to say that I was calling GC.Collect in my tests before to > make a memory snapshot. I know that Dispose won't cause the object to be > garbage collected immediatly. > > So : > 1) Not settings the Tag property to null, I can see in the memory the > disposed control and some of my classes instances (after GC.Collect). > 2) Setting the Tag proprty to null, every thing disappear (after > GC.Collect) collected. The GC still decides how extensive the collection should be, and if it's worth it at all. If there is too little that would be collected, it might decide to skip the collection altogether. So, calling GC.Collect can not really be used as a diagnostic tool. Quote: > According to your explainations, it means, I think, that there were > somewhere some references to the control in my classes (I will have a > look and correct this). A kind of circular reference that I broke > setting the Tag property to null and permit the garbage collector to do > its job ? active references, so two objects that reference each other but are not referenced from somewhere else, are still recognised as unreachable. Quote: > So, about my question. Isn't it a best practice, if I develop a custom > control, to set all it's possible extern references (as the Tag > property) to null when disposing ? as the referene will soon be unreachable anyway. -- Göran Andersson _____ http://www.guffa.com |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Blue Screen memory managment | Live Messenger | |||
| Process managment | PowerShell | |||
| Vista good memory managment, could it be true? | Vista General | |||
| DVD-drive power managment | Vista hardware & devices | |||
| Disk Managment | Vista performance & maintenance | |||