Created
June 13, 2011 12:44
-
-
Save dgrunwald/1022708 to your computer and use it in GitHub Desktop.
NR memory usage test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Xml; | |
using System.Xml.Linq; | |
using ICSharpCode.NRefactory.TypeSystem; | |
using ICSharpCode.NRefactory.TypeSystem.Implementation; | |
namespace TestNRMemoryUsage | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
LoadStuff(SharedLoader()); | |
Test("Without interning: ", () => new CecilLoader()); | |
Test("With interning: ", () => new CecilLoader { InterningProvider = new SimpleInterningProvider() }); | |
Test("Shared interning: ", SharedLoader()); | |
Console.Write("Press any key to continue . . . "); | |
Console.ReadKey(true); | |
} | |
static Func<CecilLoader> SharedLoader() | |
{ | |
CecilLoader loader = new CecilLoader(); | |
loader.InterningProvider = new SimpleInterningProvider(); | |
return () => loader; | |
} | |
static void Test(string name, Func<CecilLoader> loader) | |
{ | |
long startMemory = GC.GetTotalMemory(true); | |
var x = LoadStuff(loader); | |
loader = null; | |
long endMemory = GC.GetTotalMemory(true); | |
GC.KeepAlive(x); | |
Console.WriteLine(name + (endMemory - startMemory) / 1024 + " KB"); | |
} | |
static CompositeTypeResolveContext LoadStuff(Func<CecilLoader> loader) | |
{ | |
List<ITypeResolveContext> list = new List<ITypeResolveContext>(); | |
list.Add(loader().LoadAssemblyFile(typeof(object).Assembly.Location)); | |
list.Add(loader().LoadAssemblyFile(typeof(Uri).Assembly.Location)); | |
list.Add(loader().LoadAssemblyFile(typeof(Enumerable).Assembly.Location)); | |
list.Add(loader().LoadAssemblyFile(typeof(XmlDocument).Assembly.Location)); | |
list.Add(loader().LoadAssemblyFile(typeof(XDocument).Assembly.Location)); | |
list.Add(loader().LoadAssemblyFile(typeof(System.Windows.Application).Assembly.Location)); | |
list.Add(loader().LoadAssemblyFile(typeof(System.Windows.DependencyObject).Assembly.Location)); | |
list.Add(loader().LoadAssemblyFile(typeof(System.Windows.Clipboard).Assembly.Location)); | |
list.Add(loader().LoadAssemblyFile(typeof(System.Windows.Markup.ArrayExtension).Assembly.Location)); | |
return new CompositeTypeResolveContext(list); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment