I created few static list for the records which are being used as look up during application cycle.
However when inspected on the memory profiler and see the object context is being disposed however the GC cannot collect the memory because it keeps in references.
Following is the code snippet (I thought using AsNoTracking would break the relationship between context and entity and would allow context to die in piece.)
private static List<State> _states;
public static List<State> States
{
get
{
if (_states == null)
LoadStates();
return _states;
}
}
private static void LoadStates()
{
using (LeadContextUoW leadContext = new LeadContextUoW())
{
_states = leadContext.States.AllWithNoTracking.ToList();
}
}
Please let me know what is the wrong with this code that is causing memory leak.