问题描述
从this question我了解到
- 结构可以在堆栈或寄存器中分配,而不是在堆上
- 如果结构体是堆上引用类型对象的一部分,则结构体也将在堆上
但是一个结构体不是对象的一部分,而是类的静态成员,像这样:
public class Program
{
public static CustomStructType inst1;
static void Main(string[] args)
{
//assigning an instance of value type to the field
inst1 = new CustomStructType();
}
}
public struct CustomStructType
{
//body
}
堆上不会有 Program
的实例。那么结构体将存储在哪里?
这个问题是 this deleted question 的改写版本。该用户已被删除,因此问题和答案随之而来。我还是觉得这个想法很有趣,调试结果更有趣,所以我选择在这里重复一遍。
关于潜在的重复:
- this question 创建一个类的实例。如前所述,我知道作为对象的一部分存储的结构在堆上。我的代码没有创建类的实例。
- this question 不管它是否是静态的,都让它保持打开状态,答案是“不,如果你在 Main 内部这样做,一般来说,它不会在堆上分配。”
-
this question 有 Jon Skeet 的一个很好的答案,它说每个
new
在堆栈上分配空间。
解决方法
也许你错过了:Eric Lippert 在旁注中提到过:
[...] 和静态变量存储在堆上。
这是在上下文中写的
事实是,这是一个实现细节 [...]
Microsoft 的实现方式如下:
但是为什么静态变量存储在堆上?
好吧,即使是 Main() 方法也不会永远存在。 Main() 方法可能会结束,而其他一些线程可能仍在运行。在这种情况下,结构应该发生什么?它不一定在堆上,但我希望您看到它不能在堆栈上,也不能在寄存器中。该结构必须位于其他线程仍然能够访问它的某个地方。堆是一个不错的选择。
Main() 终止的代码示例:
using System;
using System.Threading;
public class Program
{
public static CustomStructType inst1;
static void Main(string[] args)
{
new Thread(AccessStatic).Start();
//assigning an instance of value type to the field
inst1 = new CustomStructType();
Console.WriteLine("Main is gone!");
}
static void AccessStatic()
{
Thread.Sleep(1000);
Console.WriteLine(inst1);
Console.ReadLine();
}
}
public struct CustomStructType
{
//body
}
让我们回到你的原始代码。如有疑问,您可以随时使用调试器进行检查。这是 .NET Framework 4.8 (4.8.4341.0) 中发布版本的调试会话。
我正在使用 WinDbg Preview 进行调试,这是 Microsoft 提供的免费调试器。不过使用起来并不方便。我是从 "Advanced .NET debugging" by Mario Hewardt 一书中了解到的。
为了简单起见,我插入了一个 Console.ReadLine()
,因此我不需要遍历所有内容并在正确的时间停止。
加载 .NET 扩展
ntdll!DbgBreakPoint:
77534d10 cc int 3
0:006> .loadby sos clr
搜索Program
的实例(只是为了检查问题的前提是否正确)确实给出了0个对象:
0:007> !dumpheap -type Program
Address MT Size
Statistics:
MT Count TotalSize Class Name
Total 0 objects
搜索班级:
0:006> !name2ee *!Program
Module: 787b1000
Assembly: mscorlib.dll
--------------------------------------
Module: 01724044
Assembly: StructOnHeap.exe
Token: 02000002
MethodTable: 01724dcc
EEClass: 01721298 <--- we need this
Name: Program
获取课程信息:
0:006> !dumpclass 01721298
Class Name: Program
mdToken: 02000002
File: C:\...\bin\Release\StructOnHeap.exe
Parent Class: 787b15c8
Module: 01724044
Method Table: 01724dcc
Vtable Slots: 4
Total Method Slots: 5
Class Attributes: 100001
Transparency: Critical
NumInstanceFields: 0
NumStaticFields: 1
MT Field Offset Type VT Attr Value Name
01724d88 4000001 4 CustomStructType 1 static 0431357c inst1
^-- now this
检查垃圾收集堆在哪里:
0:006> !eeheap -gc
Number of GC Heaps: 1
generation 0 starts at 0x03311018
generation 1 starts at 0x0331100c
generation 2 starts at 0x03311000
ephemeral segment allocation context: none
segment begin allocated size
03310000 03311000 03315ff4 0x4ff4(20468)
Large object heap starts at 0x04311000
segment begin allocated size
04310000 04311000 04315558 0x4558(17752) <-- look here
Total Size: Size: 0x954c (38220) bytes.
------------------------------
GC Heap Size: Size: 0x954c (38220) bytes.
是的,它位于从 0x04311000 开始的大对象堆上。
顺便说一句:让我惊讶的是,这么小的“对象”(结构)会被分配到大对象堆上。通常,LOH 将仅包含 85000+ 字节的对象。但这是有道理的,因为 LOH 通常不会被垃圾回收,而且您不需要垃圾回收 static
项。