dispose vs finalize

J

Joe Abou Jaoude

I m preparing to pass the 70-306 exam, so i downloaded Q & A
from multiple sites.
There's this question that really confuses me, coz i see that both
answers A and C are both correct.
Can anyone help me to pick the right answer and explain me why ?

thx.

Your development team creates an order entry application by using
Visual Studio .NET. The application stores and retrieves data in a
Microsoft SQL Server database. All database connections in the
application
are centralized in class variables within a class named
MyDataClass.
Each time your application needs to access data from the database,
it
creates an instance of MyDataClass by using the following code
segment:

Dim oData As New MyDataClass()

When the oData variable is no longer needed, it is set to Nothing
or goes out of scope. Initially, about 500 sales
representatives use the application. Later, your company hires 50 new
sales representatives who also use the application.
You discover that the database is running out of available connections
because of the increased usage. You must ensure
that database connections are released immediately when they are no
longer needed. You must also maintain an optimum
level of application performance.
What should you do?

A. Add the procedure Protected Overrides Finalize() to
MyDataClass.
Write code in the procedure to close all open database
connections.
B. Add the procedure Private Sub Finalize() to MyDataClass.
Write code in the procedure to close all open database
connections.
C. Implement the IDisposable interface within MyDataClass. Write
code in the Dispose
procedure of IDisposable to close all open database
connections. Call the
Dispose method of MyDataClass before any reference to
MyDataClass is set
to Nothing or goes of scope.
D. Find each location in your code where a reference to
MyDataClass is set
to nothing or goes out of scope. Add code after each instance
to manually
invoke the Visual Studio.NET garbage collector.
E. Add code to the Terminate event of MyDataClass to close all
open database connections.
F. Ensure that each reference to MyDataClass is set to Nothing
before it goes of scope.

Answer: A
 
O

One Handed Man

Hi Joe,

This means that you can use this function to do your own cleanup basically
this is like the C++ destructor where you can close open connections etc
before the garbage collector takes over. The garbage collector does not
close connections when the object gets destroyed.

Dispose marks the object for collection by the garbage collector.

Regards - OHM
 
T

Tom Shelton

Hi Joe,

This means that you can use this function to do your own cleanup basically
this is like the C++ destructor where you can close open connections etc
before the garbage collector takes over. The garbage collector does not
close connections when the object gets destroyed.

Dispose marks the object for collection by the garbage collector.

Regards - OHM

In general, the Dispose pattern is used to release unmanaged resources in
a timely fashion. This allows you to do this in a deterministic fashion
- in other words, you have control over when they are released. The
Object.Finalize method is also used for the purpose of releasing
resources - but it is non-derministic. You have no control over when it
will be called by the GC. Further, implementing a Finalize method can
have an adverse affect on the performance of the GC because it takes two
passes of the GC to collect objects that override Finalize.

So, in general, if your using objects that manage unmanaged resources,
such as database connections, you implement IDisposable AND override
Finalize. This way, your covered if the client fails to call Dispose -
you know that your resources will then be released when the object is
GC'd. Of course, one you call Dispose - you don't need the finalize
method to be called by the GC and suffer an unnecessary performance
hit... So, generally you'll see the Dispose method implemented sort of
like this:

Public Sub Dispose()
' Do Clean up
System.GC.SupressFinalize(ME)
End Sub

This way, if the client does call dispose, then you don't suffer from
having implemented a Finalize because the object is removed from the
Finalize queue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top