开发者

Cache size for class with no properties and fields

Hi

I want to know, when i cache a class with no parameters of fields, how much space it takes ? Is it true that only fields and properties of a class consume space ? if it is true, when i create a class with this specification is it true that it occupies only pointer to this class in cache ? Please help me with how caching really works in terms of occupy space of a class ele开发者_运维知识库ment


An "empty" object (var obj = new object();) occupies 12 bytes (I previously had said 16 bytes) in the 32 bit runtime. It occupies 24 bytes in the 64 bit runtime.

Here's the program I use to test that.

        var objs = new List<object>(1000000);
        var memUsedStart = GC.GetTotalMemory(true);
        Console.WriteLine("Beginning memory usage = {0:N0}", memUsedStart);
        for (int i = 0; i < 1000000; ++i)
        {
            objs.Add(new object());
        }
        var memUsedEnd = GC.GetTotalMemory(true);
        Console.WriteLine("{0:N0} items in list", objs.Count);
        Console.WriteLine("Ending memory usage = {0:N0}", memUsedEnd);
        var memUsed = memUsedEnd - memUsedStart;
        Console.WriteLine("Difference = {0:N0}", memUsed);
        Console.WriteLine("Bytes per object = {0}", memUsed / 1000000);
        Console.ReadLine();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜