我班有多少内存实例使用 - 务实答案有多少、实例、内存、答案

2023-09-03 05:30:53 作者:涉嫌想见你

在多大下面的类的实例构造函数被调用后? 我想这一般可以写为大小= NX + C,其中x = 4 在86,并且x = 8 64。 N =? C =? 有没有在.NET中的一些方法,它可以返回这个数字? How big is instance of following class after constructor is called? I guess this can be written generally as size = nx + c, where x = 4 in x86, and x = 8 in x64. n = ? c = ? Is there some method in .NET which can return this number?

类节点 {     字节[] []一个;     INT [] B:     名单<节点> C;     公共节点()     {         一个新的=字节[3] [];         B =新INT [3];         C =新的名单,其中,节点>(0);     } }

class Node { byte[][] a; int[] b; List<Node> c; public Node() { a = new byte[3][]; b = new int[3]; c = new List<Node>(0); } }

推荐答案

首先,这一切都取决于这个程序被编译和运行环境,但如果你修复一些变量,你可以得到pretty的很好的猜测。

First of all this depends on environment where this program is compiled and run, but if you fix some variables you can get pretty good guess.

答2)是NO,没有任何功能,将让你在给定的参数中的任何对象要求的答案。

Answer to 2) is NO, there is no function that will give you requested answer for any object given as argument.

在解决1)你有两种方式:

In solving 1) you have two approaches:

尝试进行一些测试,以找出 分析对象,做数学题

先来看看这些:

what-is-the-memory-overhead-of-a-net-object 开销? C#列表大小VS双[]尺寸 what-is-the-memory-overhead-of-a-net-object Overhead of a .NET array? C# List size vs double[] size

您需要的方法是这样的:

Method you need is this:

const int Size = 100000;
private static void InstanceOverheadTest()
{
    object[] array = new object[Size];
    long initialMemory = GC.GetTotalMemory(true);
    for (int i = 0; i < Size; i++)
    {
        array[i] = new Node();
    }
    long finalMemory = GC.GetTotalMemory(true);
    GC.KeepAlive(array);
    long total = finalMemory - initialMemory;
    Console.WriteLine("Measured size of each element: {0:0.000} bytes",
                      ((double)total) / Size);
}

在我的Windows 7计算机,VS 2012,.NET 4.5,86(32位)结果是96.000。当改到x64的结果是176.000。

On my Windows 7 machine, VS 2012, .NET 4.5, x86 (32 bit) result is 96.000. When changed to x64 result is 176.000.

做可以写成一个函数,它会给你造成的,但具体的为你Node类的数学方法,并执行你的对象等操作前,它才有效。另请注意,这是在32位程序,并还注意到,这个数字可与框架的实施和版本更改。这只是例子,你如何可以提供有关对象大小pretty的很好的猜测在某些时刻,如果对象是很简单的。数组和列表的开销常数取自.NET阵列开销?和 C#列表大小VS双[]尺寸

Do the math approach can be written as a function that will give you result, but is specific for your Node class, and it is only valid before other operations on your object are performed. Also notice that this is made in 32-bit program and also note that this number can change with framework implementation and version. This is just example how you can give pretty good guess about object size in some moment if object is simple enough. Array and List overhead constants are taken from Overhead of a .NET array? and C# List size vs double[] size

public const int PointerSize32 = 4;
public const int ValueArrayOverhead32 = 12;
public const int RefArrayOverhead32 = 16;
public const int ListOverhead32 = 32;
private static int instanceOverheadAssume32()
{
    int sa = RefArrayOverhead32 + 3 * PointerSize32;
    int sb = ValueArrayOverhead32 + 3 * sizeof(int);
    int sc = ListOverhead32;
    return 3 * PointerSize32 + sa + sb + sc;
}

这也将返回96,所以我认为方法是正确的。

This will also return 96 so I assume that method is correct.