While it's quite hard to believe that an assembly of enum
s could indeed be the main source of high memory usage (I don't even want to imagine the SIZE of that assembly), some possible solutions flow into mind:
- Exploring your memory-greedy
enum
assembly for things other thanenum
s. Yes, it is a trivial advice, but it could prove the most reasonable after all. - Merging your
enum
assembly with main project. Whileenum
s by themselves are hardly memory consuming, associated oversized assembly could well be all by itself (because of heavy usage of attributes, for instance). - Implementing necessary values with a bunch of not-necessarily
static
classes, filled with a bunch ofpublic static readonly int
fields (notconst
s). Hardly will reduce memory usage if all of them are called at least once, but could lead to some reduction depending on user's actions. However, those peskyint
s are harder to account for thenenum
s. Implementing necessary values with cached singleton-ish pattern (perhaps there is a name for such a pattern, but I am not familiar with it). While obviously harder on memory on a per-instance basis, it allows (while depending on GC) to be put into life and snuffed out of it based on usage. Basically, it should be something like
internal abstract class ErzatsEnumBase { protected static readonly ObjectCache Cache = MemoryCache.Default; protected ErzatsEnumBase ( string CacheKey, CacheItemPolicy CachePolicy ) { Cache.Add ( CacheKey, this, CachePolicy, null ); } } public sealed class ErzatsEnum: ErzatsEnumBase { private static CacheItemPolicy policy = new CacheItemPolicy () { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration, SlidingExpiration = new TimeSpan ( 0, 15, 0 ) }; private ErzatsEnum ( string CacheKey ) : base ( CacheKey, policy ) { } public ErzatsEnum Instance1 { get { ErzatsEnum result = ErzatsEnumBase.Cache.Get ( "Instance1" ); if ( result == null ) result = new ErzatsEnum ( "Instance1" ); return result; } } public ErzatsEnum Instance2 { get { ErzatsEnum result = ErzatsEnumBase.Cache.Get ( "Instance2" ); if ( result == null ) result = new ErzatsEnum ( "Instance2" ); return result; } } }
Hope some of it proved useful :)