Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cost to GC of using weak references in C#?

In another question, Stephen C says:

A second concern is that there are runtime overheads with using weak references. The obvious costs are those of creating weak references and calling get on them. A less obvious cost is that significant extra work needs to be done each time the GC runs.

So what exactly is the cost to the GC of a weak ref? What extra work does it need to do, and how big of a deal is it? I can make some educated guesses, but am interested in the actual mechanics.

like image 897
scobi Avatar asked May 14 '10 22:05

scobi


People also ask

When should weak references be used in garbage collection?

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.

What is weak reference in C?

A weak reference allows the garbage collector to collect an object while still allowing an application to access the object. If you need the object, you can still obtain a strong reference to it and prevent it from being collected.


1 Answers

Please check Jeffrey Richter's article about Memory Management in .NET it must clear up things a little.

like image 73
Incognito Avatar answered Sep 21 '22 13:09

Incognito