GC probably starts and frees up memory ... for the application itself. That is, if the call to Sleep() requires some RAM, then it will probably find a lot of it, namely the large blocks that were originally allocated for the huge Dictionary .
This does not mean that GC returns memory to the operating system. From an OS perspective, large blocks can still be part of the process, rather than being used by any other process.
Distribution is not optimized because it is some kind of external code. Your Main class calls the constructor for Dictionary<int,int> , which can do its best with various side effects. As a human programmer, you expect that the constructor will not have externally visible side effects, but the compiler and the virtual machine do not know this for sure. Thus, the code cannot do without creating an instance of Dictionary<int,int> and calling its constructor. Similarly, the Dictionary<int,int> constructor does not know that it is called for an object that will soon become inaccessible, so it will not be able to optimize itself.
source share