Why is GC.GetTotalMemory () reporting huge amounts of memory?

I played with GC.GetTotalMemory (). When I create a local variable of type Titles in the example below, the memory consumption increases by 6276 bytes. What's going on here?

class Program { enum Titles { Mr, Ms, Mrs, Dr }; static void Main(string[] args) { GetTotalMemory(); Titles t = Titles.Dr; GetTotalMemory(); } static void GetTotalMemory() { long bytes = GC.GetTotalMemory(true); Console.WriteLine("{0}", bytes); } } 
+7
c #
source share
1 answer

I think this is because the allocator somewhere bites most of the memory. He will use it for several objects. Try to do:

 GetTotalMemory(); Titles t = Titles.Dr; GetTotalMemory(); Titles t2 = Titles.Mr; GetTotalMemory(); 

and see what happens.

this is what I see, and GetTotalMemory () is not so innocent:

  GetTotalMemory(); Titles t = Titles.Dr; GetTotalMemory(); 

outputs:

 12828 19484 

and this:

 GetTotalMemory(); //Titles t = Titles.Dr; GetTotalMemory(); 

outputs:

 12828 19484 

in fact, you should not pay attention to small fluctuations in free memory:

Then he tells them: therefore, to Caesar what Caesar is; and to God the things that are God's

:)

+9
source share

All Articles