I read something about the Weakreference Class in C#, but I´m not sure about the usage of it.
I have a huge table with over 10 or 20k entries. When I write for example a List DBFoos, where Foo are objects from the database.
Where is the advantage of using Weakreference instead of Factory/Singelton Pattern if the user does read only and the pattern is only for the communitation between the classes, because the download of objects takes a while?
Edit
I found an example on MSDN
They use a Weakreference Dictionary like:
public class Cache
{
// Dictionary to contain the cache.
public static Dictionary<int, WeakReference> _cache;
If I set in this Cache my Object with the List instead of using a Singelton Like:
public class Foo{
private static Foo foo;
static Foo getreference(){
if(foo == null){
foo = new Foo();
return foo;
}
else
return foo;
}
}
Is there any advantage or disatvantage like collecting by the GC with using Weakreference or is it only a complex way of saving it ?
Weakreference is used to make a cache, you store weakreference in cache to let garbage collector to do is job, so some time you have to reload from database some elements. With Singleton it will make static cache which is not garbaged but if there is enough memory it works fine.
Is there a practical use for weak references?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With