![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Question about Class Destruction I've been using .net for a while now as novice user and I understand the concept of the garbage collection to a degree. My question is simple and I'm sure someone out there can help me. Is it good practice to destroy a class when when it's no longer needed? for example if I have the following code within a procedure: myClass a = new myClass(); Try { Class work... } catch { } finally { a = null; } Is it good practice to destroy the class? In my code I want to be efficient as possible and I'm looking for real good practices. If this was a COM object I'm assuming that it would be good to destroy the object because garbage collection sees this as unsafe code. Correct or incorrect? -- TC |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Question about Class Destruction Terrance wrote: Quote: > I've been using .net for a while now as novice user and I understand the > concept of the garbage collection to a degree. My question is simple and I'm > sure someone out there can help me. Is it good practice to destroy a class > when when it's no longer needed? things well separated!) What it has is automatic memory management and finalization (which frees up anything that's not under the purview of the CLR, i.e. resources that are not memory). When an object is garbage collected, it will be queued for finalization if it needs to be finalized, and afterwards its memory will be freed. Finalizers come closest to destructors in terms of functionality (but not use). The MSDN has an excellent if somewhat overwhelming overarching topic: http://msdn2.microsoft.com/library/0xy59wtx You should especially read the first two entries; the rest is advanced stuff you won't encounter very often. Quote: > for example if I have the following code > within a procedure: > > myClass a = new myClass(); > > Try > { > Class work... > } > catch > { > } > finally > { > a = null; > } > > Is it good practice to destroy the class? has no effect on the ability of the CLR to collect objects that are no longer referenced. (This is not entirely true, but when you understand why it's not entirely true you won't need this advice anymore, so I'm skipping the details.) In particular, it will not force the object to be collected and it will not cause unmanaged resources to be released. Code like this is especially popular among programmers that are migrating from VB (since this was the accepted way of releasing COM objects), but it should not be used in .NET. Instead, classes whose instances hold resources that need to be freed in a deterministic manner should implement IDisposable, and clients should take care to call Dispose() on objects they no longer need. A useful shorthand for this is the using statement: using (MyClass a = new MyClass()) { ... } which is roughly equivalent to: MyClass a; try { a = new MyClass(); ... } finally { if (a != null) a.Dispose(); } See the documentation on IDisposable for more information, and "Implementing Finalize and Dispose to Clean Up Unmanaged Resources" (http://msdn2.microsoft.com/library/b1yfkh5e). Note that most classed do not need finalization, do not need to implement IDisposable and in short do not need to do anything special for garbage collection to "just work". Quote: > In my code I want to be efficient as possible and I'm looking for real > good practices. If this was a COM object I'm assuming that it would be > good to destroy the object because garbage collection sees this as unsafe > code. Correct or incorrect? (http://msdn2.microsoft.com/library/t2yzs44b), not to be confused with "unmanaged resources". It is true that the COM object will probably take up unmanaged resources, and if you cannot wait for these to be disposed when the COM object is released (which will happen when it's garbage collected) you can force a release through the special method Marshal.ReleaseComObject(). This is particular to COM objects, however, and does not apply in general. -- J. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Question about Class Destruction On Wed, 9 Apr 2008 13:00:01 -0700, Terrance <Terrance@xxxxxx> wrote: Quote: >I've been using .net for a while now as novice user and I understand the >concept of the garbage collection to a degree. My question is simple and I'm >sure someone out there can help me. Is it good practice to destroy a class >when when it's no longer needed? for example if I have the following code >within a procedure: > >myClass a = new myClass(); > >Try >{ > Class work... >} >catch >{ >} >finally >{ > a = null; >} > >Is it good practice to destroy the class? In my code I want to be efficient >as possible and I'm looking for real good practices. If this was a COM object >I'm assuming that it would be good to destroy the object because garbage >collection sees this as unsafe code. Correct or incorrect? all references to it (assuming nothing in the Try block stores the reference to the instance somewhere else) which makes it eligible for garbage collection. If the scope of 'a' is local to a method, and there is little execution time from the point where you no longer need the instance to the end of the method, then my understanding is that there is no need to set the reference to null, and in some cases it might be counter-productive. If the scope of the variable holding the reference was for more than just the current method, then I would clear the reference when it was no longer needed, but I think such a case would be unusual. Of course if the class has unmanaged resources, then you would want to call the class's Dispose method, either explicitly or by putting it in a 'using' block, as soon as you are finished with it. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Question about Class Destruction Quote: > Is it good practice to destroy the class? Cor |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Question about Class Destruction Thanks, for the information. You are correct; I was creating a reference. -- TC "Cor Ligthert[MVP]" wrote: Quote: > Quote: > > Is it good practice to destroy the class? > The same good practise as smoking a cigar after dinner. > > Cor > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| When a class is both an inherited class of another, and alsoimplements an interface method | .NET General | |||
| win32_pingstatus class / dns class | PowerShell | |||
| Question from .net beginner regarding rebuilding custom class library | .NET General | |||
| wmi win32_scheduledjob class | PowerShell | |||