0

I have been doing a lot of research about static objects and the high vs low frequency heap. I understand that the high frequency heap is not garbage collected. My question is if I am instantiating an array or dictionary in a normal class as a static instance in a web server:

public class Lookup 
{
    private static readonly Lookup instance = new Lookup();
    private Dictionary<Decimal, Enum> _enums;
    public static Enum GetEnum(Decimal value)
    {
        return instance._enums[value];
    }
    Lookup()
    {
        _enums = new Dictionary<decimal, Enum>();
        _enums[1.1] = enum.enum1;
        _enums[2.2] = enum.enum2;
        _enums[3.3] = enum.enum3;
        _enums[4.4] = enum.enum4;
        //...etc
        _enums[5000] = enum.enum5000;
    }
}

The static instance is placed in High Frequence heap. Is the dictionary there too? Or maybe is there a pointer to the low frequency heap?
When creating a dictionary/array of default size and adding the values one at a time, the dictionary has to re-size itself repeatedly. As I understand it, it does this by allocating new memory and copying over the data, and the old dictionary gets Garbage Collected eventually. But if the High frequency heap is not collected, would this cause multiple copies of the array to reside in the HF heap? Or if the HF heap has only pointers to the LF heap, do they not get collected because there are references residing in HF heap still?

Edit: Thanks Alexei Levenkov, this is currently running on the web and we are getting really unusual out of memory exceptions killing our server when this GetEnum is called. As to the references, here are a few:

http://www.codeproject.com/Articles/15269/Static-Keyword-Demystified

High Frequency Heap

http://msdn.microsoft.com/en-us/magazine/cc163791.aspx

trincot
  • 317,000
  • 35
  • 244
  • 286
BigDCoder
  • 45
  • 9
  • 1
    Link that explains that normal objects are placed on high frequency heap (or just links to where you get your information about this heap) would be useful. Also explanation why it is important for your code may be beneficial to this question. – Alexei Levenkov Aug 14 '13 at 22:50
  • 3
    No, the dictionary is still in the normal GC heap. Only the *reference* is static. You are looking in the wrong corner for this problem. – Hans Passant Aug 14 '13 at 23:33
  • @HansPassant well, when I change this to creating a new instance every time it's called(remove static) it starts working. Also, by changing this to a lazy-load singleton with a lock, it seems to have made a difference too. Unusual seeing as this is only assigned once, after that it's only read. – BigDCoder Aug 14 '13 at 23:45

0 Answers0