How do I migrate source-code that uses java.lang.ref.ReferenceQueue? .NET is only offering System.WeakReference without the possibility of getting a notification when a weak-reference has been collected. A ReferenceQueue is essential when programming a WeakHashtable, e.g. the values in the Hashtable are WeakReferences and I want to remove the (complete) entry from the Hashtable as soon as the object the WeakReference is pointing to is collected.
Microsoft .NET does not have a direct functional equivalence for Java’s Reference Objects behavior. Only a little functionality of Java’s Reference Objects can be achieved through Microsoft .NET’s System.GC and System.WeakReference classes. These classes provide controlling and monitoring the activities and data structures of Microsoft .NET’s garbage collector. However, Microsoft .NET’s garbage collection model does not provide notification for collection.
Some of Java’s ReferenceQueue functionality could be provided by controlling the .NET’s reference tables:
- Finalization Queue: A list of references to the objects on the heap that need to be finalized
- Freachable Queue: A list of references to the objects on the heap that are awaiting finalization
- Short Weak References: A list of references to the objects on the heap that are weakly referenced
- Long Weak References: Similar to the short table but tracks are long weak references
Principally, System.GC provides the following methods to access Finalization and Weak References tables: ReRegisterForFinalize and SuppressFinalize, and WaitForPendingFinalizers, respectively.
One suggestion would be to review your target application in order to redesign a workaround for the involved functionality. You may consider the workaround - to "be notified" when an object is collected - by overriding the Finalize method and handle the "notification" inside this method (since Finalize method is called when the object is collected). This could be used to maintain synchronization of the corresponding data structure. However, this is dependant on the feasibility to override the Finalize method in all owner classes of the objects managed by the data structure.
A detailed example and walkthrough is available here