开发者

Why does GC.GetTotalMemory() report huge memory allocations?

开发者 https://www.devze.com 2022-12-25 05:05 出处:网络
I have been playing around 开发者_JAVA百科with GC.GetTotalMemory(). When I create a local variable of type Titles in the example below, the consumed amount of memory increases by 6276 bytes. What\'s g

I have been playing around 开发者_JAVA百科with GC.GetTotalMemory(). When I create a local variable of type Titles in the example below, the consumed amount of memory 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);
    }
}


I think it is because allocator somewhere bite a big piece of memory. It will use it for more than one object. Try to do:

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

and see what happens.

here 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

actually you should not pay attention to small fluctuations of free memory:

Then saith he unto them, Render therefore unto Caesar the things which are Caesar's; and unto God the things that are God's

:)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号