|
From: Jim A. <JA...@th...> - 2004-06-21 15:22:08
|
One of NMock's more subtle 'features' is that every time you create a new DynamicMock instance, a brand new dynamic assembly is created containing the mock type. This assembly is not reused, and neither are the mock types, so the following: new DynamicMock(typeof(IThingy)); new DynamicMock(typeof(IThingy)); results in two new assemblies, each containing one identical class. The problem is that assemblies are never unloaded from a live AppDomain, so for long-lived domains (ie NUnit test runs), NMock keeps chewing up memory. I've attached a fix for this (source files only - you'll need to include the new files in the correct VS projects). It involves 1) making the assembly (the ModuleBuilder, to be specific) static, so we can reuse it throughout the entire test run, and 2) caching mock types, so we never create two identical mock types for the same parent type. Putting this fix in resulted in a ~50% reduction in memory and a ~10% increase in speed for us (that's for ~2000 tests). Jim (See attached file: MockTypeIdentifier.cs)(See attached file: ClassGenerator.cs)(See attached file: MockTypeIdentifierTest.cs)(See attached file: ClassGeneratorTest.cs) |