Reputation: 143
The question is simple and can be summed up by:
How can I get this while loop to exit?
System.Windows.Media.MediaPlayer player = new System.Windows.Media.MediaPlayer();
WeakReference test = new WeakReference(player);
player.Close();
player = null;
while (test.IsAlive && test.Target != null)
{
System.GC.Collect();
}
I have searched the documentation and have found no way to dispose of this object, the while loop never exits.
Upvotes: 5
Views: 1956
Reputation: 59228
I am surprised that neither the C# documentation nor the Java documentation describes it that clearly:
If there is no memory pressure, a WeakReference will not be collected.
This MSDN article might be helpful:
A weak reference permits the garbage collector to collect the object while still allowing the application to access the object.
Permission is not a requirement. It may do so, but it needn't.
Wikipedia gets it right under Variations (for Java, but Java and C# are quite similar):
[...] the GC may decide not to do so if it believes the JVM can spare the memory (e.g. the JVM has lots of unused heap space)
Upvotes: 2