DynamoDS/Dynamo — pull request #2538 by ke-yu (2014-09-26 10:14:31+00:00)
Title: Fix event leak in Dynamo Core

Notice memory usage keeps on growing when running nunit on DynamoCoreTests, this pull request tries to release DynamoModel after running a test case.

Memory leak is mostly because of [event handler leak] (http://blogs.msdn.com/b/abhinaba/archive/2009/05/05/memory-leak-via-event-handlers.aspx), but .NET GC actually is smart enough to deal with that, unless the publisher lives long time. For example, the publisher is a static class. In our case, they are ``DynamoSelection`` and ``UsageReportingManager`` and some other classes like ``DynamoViewModel`` registers event handler on them. As ``DynamoViewModel`` also references to ``DynamoModel``, so ``DynamoModel`` instance resides in the memory after running test case.

Ideally calling ``DyanmoSelection.DestroyInstance()`` should make .Net garbage collection properly collect all subscribers. But here I still manually remove event handlers in ``DynamoViewModel`` (otherwise ``DynamoViewModel`` won't be garbage collected -- need more investigation).

Unfortunately, even now no more ``DynamoModel`` in the heap, .Net memory still keeps on going up to over some gigabyte. Forcing GC (in memory profiling tool) sometimes brings memory footprint down but sometimes doesn't. But anyway after this change, dotMemory doesn't detect memory leak in DynamoCore.

@ikeough , @Benglin , you may help to review the change. Thanks.