相关疑难解决方法(0)

.NET数组的开销?

我试图使用以下代码确定.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)

.net c# arrays overhead

36
推荐指数
2
解决办法
7944
查看次数

在64位处理器上为3点结构分配了多少字节?

有一个问题:

鉴于:

struct Point {int x; int y;}
var p = new Point[3]
Run Code Online (Sandbox Code Playgroud)

如果我们使用64位处理器,将在堆栈和堆中分配多少字节的内存?

正确答案.Net44.有人可以解释这个数字是如何出现的吗?

据我所知,p将占用堆栈中的8个字节x64.

并且我们有Int32每个结构的两个值,因此 堆中的p.Length * sizeof(Point) 3*8 = 24个字节用于数组.

这将是32个字节.在这种情况下剩下的12个字节是多少?

.net c# memory arrays struct

24
推荐指数
2
解决办法
2246
查看次数

标签 统计

.net ×2

arrays ×2

c# ×2

memory ×1

overhead ×1

struct ×1