Classically WeakReferencing is used for relatively large memory objects which are not time consuming to create. By weak-referencing such objects we let GC know that it is okay to wipe out despite the active reference; if the referenced object is called again then it is created again. Here is the blog link . The benefit is to avoid keeping your memory occupied and putting it at use for other stuff and when the weak reference is used again then it is created again (which presumably has to be fast).
This stackoverflow post explains why it isn't going to cause memory leaks (even if there is a possible circular reference).
Following is the code snippet which uses your parent and child classes and creates a huge demonstrator object then later sets it to null. The process is repeated three times and memory is measured at different stages. After initial memory bump, all other pre-creation post-creation and post-null readings don't change which means no memory leaking!.
class Program
{
static void Main(string[] args)
{
MonitorMemoryUsage();
MonitorMemoryUsage();
MonitorMemoryUsage();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
private static void MonitorMemoryUsage()
{
DisplayMemoryAfterGc("Before creation");
var demonstrator = new Demonstrator();
DisplayMemoryAfterGc("After creation");
if (demonstrator.Children != null && demonstrator.Children.Count > 0)
{
demonstrator.Children = null;
demonstrator.Parents = null;
demonstrator = null;
}
Console.WriteLine(demonstrator == null);
DisplayMemoryAfterGc("After null");
}
private static void DisplayMemoryAfterGc(string eventType)
{
GC.Collect();
GC.WaitForFullGCComplete();
var totalMemory = GC.GetTotalMemory(true);
Console.WriteLine(eventType + ":" + totalMemory);
}
}
public class Demonstrator
{
public List<Parent> Parents { get; set; }
public List<Child> Children { get; set; }
public Demonstrator()
{
Parents = new List<Parent>();
Children = new List<Child>();
for (int i = 0; i < 10000; i++)
{
var parent = new Parent();
Parents.Add(parent);
Children.Add(parent._child1);
}
}
}
public class Parent
{
public Child _child1, _child2;
void someFunc()
{
_child1 = new Child();
_child2 = new Child();
_child1.handle += parentHandle;
}
void parentHandle(Child sender)
{
Console.WriteLine("Sender is: " + sender.ToString());
}
}
public class Child
{
public OnHandle handle;
public delegate void OnHandle(Child sender);
void someChildFunc()
{
handle(this);
}
}