我试图使用以下代码确定.NET数组(在32位进程中)标头的开销:
long bytes1 = GC.GetTotalMemory(false);
object[] array = new object[10000];
for (int i = 0; i < 10000; i++)
array[i] = new int[1];
long bytes2 = GC.GetTotalMemory(false);
array[0] = null; // ensure no garbage collection before this point
Console.WriteLine(bytes2 - bytes1);
// Calculate array overhead in bytes by subtracting the size of
// the array elements (40000 for object[10000] and 4 for each
// array), and dividing by the number of arrays (10001)
Console.WriteLine("Array overhead: {0:0.000}",
((double)(bytes2 - bytes1) - …Run Code Online (Sandbox Code Playgroud) 有一个问题:
鉴于:
Run Code Online (Sandbox Code Playgroud)struct Point {int x; int y;} var p = new Point[3]如果我们使用64位处理器,将在堆栈和堆中分配多少字节的内存?
正确答案.Net是44.有人可以解释这个数字是如何出现的吗?
据我所知,p将占用堆栈中的8个字节x64.
并且我们有Int32每个结构的两个值,因此
堆中的p.Length * sizeof(Point)
3*8 = 24个字节用于数组.
这将是32个字节.在这种情况下剩下的12个字节是多少?