473,549 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dispose vs finalize

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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
4 5064
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


Joe Abou Jaoude wrote:
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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #2
On 2003-11-27, One Handed Man <Bo****@Duck.ne t> wrote:
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


Joe Abou Jaoude wrote:
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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



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.Supre ssFinalize(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.
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3
Thanks for that more in depth reply.

Regards - OHM

Tom Shelton wrote:
On 2003-11-27, One Handed Man <Bo****@Duck.ne t> wrote:
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


Joe Abou Jaoude wrote:
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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



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.Supre ssFinalize(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.

Nov 20 '05 #4


hi,
thank u guys for ur help.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1914
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or release refrence from memory ? E. If it release object from memory then what GC does ? 2. Which class can have dispose method ? class which is...
24
7631
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I...
4
6719
by: Sunit Joshi | last post by:
Hello All I have an abstract class C1 with this: public abstract class C1 { protected bool m_Dirty; protected override void Dispose(bool disposing) { if(m_Dirty) WriteOuput();
6
1791
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged resource? I understand the basic definition for managed resources are resources that the CLR manage and unmanged resources are resources that the CLR...
156
5774
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I...
3
1943
by: Boni | last post by:
Dear all, can somebody explain difference between Dispose and Finalize and when each of them should be used? Thank you very much, Boni
2
1285
by: eBob.com | last post by:
I have a user control which creates an Excel spread sheet and badly needs Dispose/Finalize. I've read up on the subject in Balena and I think I understand what is going on. But the VS generated coded confuses me a bit. VS generates a Dispose subroutine, but it's not obvious if it is ok to modify it. In the New subroutine VS makes clear...
6
3571
by: Water Cooler v2 | last post by:
I heard from someone that we must not implement IDisposable for all classes. Can someone please tell me: 1. the reason why we must not implement IDisposable for all the classes we write. 2. what is the correct way of implementing IDisposable.Dispose? 3. what is the preferred way, if there is one over the other, of calling the Dispose...
4
2022
by: BLUE | last post by:
I've read many articles including the one from Duff's blog but I've many doubts. public static myClass Instance { get { if (myClass.instance == null) myClass.instance = new myClass();
0
7965
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.