Destruktory VB NET


Implement the Dispose/Finalize Pattern (from http://www.fawcette.com/vsm/ - Visual Studio Magazine)
.NET objects don't have a Class_Terminate event firing when you set the last reference to them to Nothing. Instead, they can implement a (protected) Finalize method, which the .NET Framework invokes when the object is about to be garbage-collected. You must use this method when the class wraps or uses an unmanaged resource. For example, Finalize uses the OpenClipboard API to open the clipboard, and therefore must call the CloseClipboard API to release the clipboard so other processes can use it:

Class TestObject
' put the garbage collector under pressure
Dim dummyArr(1000) As Byte

Sub New()
OpenClipboard(0)
End Sub

Protected Overrides Sub Finalize()
' close the clipboard when finalized
CloseClipboard()
MyBase.Finalize()
End Sub
End Class

You can omit the call to MyBase.Finalize if the object derives from System.Object, as in this case, because the Finalize method in System.Object does nothing.

There are two problems with the previous implementation, though. First, the garbage collector calls the Finalize method sometime after the object has become unreachable from the main app. This keeps the clipboard open until the next garbage collection, and no other process is able to open it until then. Second, all objects that expose a Finalize method require at least two garbage collections (often more) before they vacate the managed heap completelynot an optimal usage of the available memory.

You can kill both birds with one stone by implementing the IDisposable interface and its Dispose method. This method should contain the same clean-up code found in the Finalize method. It's meant to be called directly from clients just before they set the object to Nothing:

Class TestObject
Implements IDisposable
Public Sub Dispose() _
Implements IDisposable.Dispose
' close the clipboard
CloseClipboard()
' no need to finalize this object
GC.SuppressFinalize(Me)
End Sub
' remainder of code as before...
End Class

The crucial pointthe GC.SuppressFinalize(Me) methodtells the .NET Framework that the object has run its clean-up code and doesn't need to be finalized. Instead, .NET removes the object completely from memory at the first garbage collection. A client should use the disposable object like this:

' create the object
Dim o As New TestObject()
' use it
' ...
' run its clean-up code and destroy it
o.Dispose()
o = Nothing

Use a Try...Finally block if there's any chance of an exception being thrown while using the object:

Dim o As TestObject
Try
' create and use the object
o = New TestObject()
' ...
Finally
' run its clean-up code and destroy it
o.Dispose()
o = Nothing
End Try

The Finalize method runs even if the object throws an exception while executing the constructor method, which might be an issue because the finalization code might access member variables that haven't been initialized correctly. You can sidestep this problem by placing the creation step inside the protected Try region.

Use my TestObject class to get an idea of the extra overhead the Finalize method requires for its extra garbage collection:

Dim i As Integer
For i = 1 To 100000
Dim o As New TestObject()
o.Dispose()
o = Nothing
Next

This code runs in 0.5 seconds on my system; if you comment out the Dispose method, however, total execution time climbs up to 1.3 seconds (two and a half times slower). The ratio depends largely on how much memory the object consumes. You can see different results if you change the size of the dummyArr array.



Wyszukiwarka

Podobne podstrony:
VB NET Module 1 Overview of the Microsoft NET Platform
VB NET Almanach vbntal
HOW TO UPGRADE AN EARLIER VB NET PROJECT (VS2003)
VB NET Introduction
VB NET Module 11 Upgrading to Visual Basic NET
VB NET Leksykon kieszonkowy
VB NET Module 10?ploying Applications
VB NET Module 10?ploying Applications
VB NET Programming with Microsoft Visual Basic NET?livery Guide
Debugowanie NET Zaawansowane techniki diagnostyczne?bnet
CSharp Introduction to C# Programming for the Microsoft NET Platform (Prerelease)
aoki densetsu shoot! e05 [saizen] (osloskop net)
net h (2)
DOD Net Centric Data Strategy and Community of Interest (COI) Training Glossary

więcej podobnych podstron