What are Weak and Strong References related to Garbage collection procedure?How does it help in performance issues?
Garbage Collector
Collapse
X
-
Tags: None
-
Data that takes lot of resouce (wrt memory) can be weak referenced (eg large table or dataset)... As long as there is strong reference to it the GC wont collect... when there is no strong reference the GC will free the resource..WeakReference Class (System) -
Having that in mind, note that WeakReference.T arget can become null at any time (GC runs on a separate thread), so writing something like this:
might throw a NullReferenceEx ception if your target gets gollected right after the tested if-expression. Always create a strong reference right before using it:Code:if (weakReference.Target != null) ((SomeClass)weakReference.Target).DoSomething();
Code:// create a strong reference to prevent GC from collecting it SomeClass object = weakReference.Target as SomeClass; if (object != null) object.DoSomething();Comment
Comment