asp net 3.5unleashed

Page 432

// A garbage collection did not occur and I can successfull y // access the Object using o. } } Why might you want to use weak references? Well, some data structures are easy to create but require a lot of memory. For example, you might have an application that needs to know all the directories and files on the user’s hard drive. You can easily build a tree that reflects this information, and as your application runs, refer to the tree in memory instead of accessing the user’s hard disk. This greatly improves the performance of your application. The problem is that the tree could be extremely large, requiring quite a bit of memory. If the user starts accessing a different part of your application, the tree might no longer be necessary but be wasting valuable memory nonetheless. You could give up the reference to the tree’s root object, but if the user switches back to the first part of your application, you’ll need to reconstruct the tree again. A weak reference allows you to handle this scenario easily and efficiently. When the user switches away from the first part of the application, you can create a weak reference to the tree’s root object and give up all strong references. If the other subcomponent’s memory load is low, the garbage collector won’t reclaim the tree’s objects. When the user switches back to the first component, the application attempts to obtain a strong reference to the tree’s root object. If successful, the application doesn’t have to traverse the user’s hard drive again. The System.WeakReference type offers two public constructors: public WeakReference(Object target); public WeakReference(Object target, Boolean trackResurrection); The target parameter identifies the object that the WeakReference object should track. The trackResurrection parameter indicates whether the WeakReference object should track the object after its Finalize method has been called. Usually, false is passed for the trackResurrection parameter, and the first constructor creates a WeakReference that doesn’t track resurrection. For convenience, a WeakReference that doesn’t track resurrection is called a short weak reference; a WeakReference that does track resurrection is called a long weak reference. If an object’s type doesn’t offer a Finalize method, short and long weak references behave identically. I strongly recommend that you avoid using long weak references. Long weak references allow you to resurrect an object after it has been finalized and the state of the object is unpredictable. Once you’ve created a weak reference to an object, you usually set the strong reference to the object to null. If any strong reference remains, the garbage collector will be unable to collect the object. To use the object again, you must turn the weak reference into a strong reference. You accomplish this by simply querying the WeakReference object’s Target property and assigning the result to one of your application’s roots. If the Target property returns null, the object was collected. If the property doesn’t return null, the root is a strong reference to the object and the code can manipulate the object. As long as the strong reference exists, the object can’t be collected.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.