This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Call Dispose() manually, this will not call the finalizer



Download 21,46 Mb.
Pdf ko'rish
bet540/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   536   537   538   539   540   541   542   543   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Call Dispose() manually, this will not call the finalizer.

MyResourceWrapper rw = new MyResourceWrapper();

rw.Dispose();

C H A P T E R   8  



U N D E R S TA N D I N G   O B J E C T   L I F E T I M E



264

8849CH08.qxd  10/22/07  1:27 PM  Page 264




// Don't call Dispose(), this will trigger the finalizer

// and cause a beep.

MyResourceWrapper rw2 = new MyResourceWrapper();

}

Notice that we are explicitly calling Dispose() on the rw object, therefore the destructor call is



suppressed. However, we have “forgotten” to call Dispose() on the rw2 object, and therefore when

the application terminates, we hear a single beep. If you were to comment out the call to Dispose()

on the rw object, you would hear two beeps.



Source Code

The FinalizableDisposableClass project is included under the Chapter 8 subdirectory.

That wraps up our investigation of how the CLR is managing your objects via garbage collec-

tion. While there are additional (fairly esoteric) details regarding the collection process I have not

examined here (such as weak references and object resurrection), you are certainly in a perfect

position for further exploration on your own terms.



Summary

The point of this chapter was to demystify the garbage collection process. As you have seen, the

garbage collector will only run when it is unable to acquire the necessary memory from the man-

aged heap (or when a given AppDomain unloads from memory). When a collection does occur, you

can rest assured that Microsoft’s collection algorithm has been optimized by the use of object gen-

erations, secondary threads for the purpose of object finalization, and a managed heap dedicated to

host large objects.

This chapter also illustrated how to programmatically interact with the garbage collector using

the System.GC class type. As mentioned, the only time when you will really need to do so is when

you are building finalizable or disposable class types that operate upon unmanaged resources.

Recall that finalizable types are classes that have overridden the virtual System.Object.

Finalize() method to clean up unmanaged resources at the time of garbage collection. Disposable

objects, on the other hand, are classes (or structures) that implement the IDisposable interface,

which should be called by the object user when it is finished using said object. Finally, you learned

about an official “disposal” pattern that blends both approaches.

C H A P T E R   8  




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   536   537   538   539   540   541   542   543   ...   1830




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish