'Memory Management for Type Instance

We are working on reflection project where we need to call different methods from different types and that too from different assemblies.

So What we are doing right now is to Load the all types from Assemblies and cache them in memory on application load to read it while reflection.

Currently we are using Dictionary<string, Type> TypesCache; which holds type name and type instance Assemblies declared types.

So I have question. Rather than keeping all types on start of application into dictionary If we maintain dictionary of Dictionary<string, string> - which will hold type name and AssemblyQualifiedName.

And run-time based on required type we will get type from AssemblyQualifiedName and then cache it in TypesCache (Like on demand)

Will memory be reduced in new implementation?

When we are preparing cache of TypesCache on application load, will it hold complete type instance or reference of type instance from Loaded Assemblies types?



Solution 1:[1]

Will memory be reduced in new implementation?

probably. A type object is likely larger that a string for the type. But the simplest method to get an accurate answer would be to do a test. Create a simple application for each alternative and check the memory used, or use a memory profiler.

A possibly more important question would be "Is the amount of memory used significant?". Do you have memory problems? Have you used a memory profiler to check what the memory is used for? Have you determined that the type cache represents a significant part of that memory? Or are you just guessing?

The first rule of optimization is to measure before you do anything. Otherwise you have no idea if you made any improvement. And it is quite common that assumptions about where the problem is are wrong.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JonasH