(注:本篇用点长,有点绕,耐心浏览)

  C#中类型分为值类型和引用类型,值类型存储在堆栈中,是栈结构,先进后出,引用类型存储在托管堆中。接下来用不安全代码的地址,来看一下值类型和引用类型的存储。

项目文件

  C#中使用不安全代码需要在项目文件中添加AllowUnsafeBlocks配置。

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>

  所有的测试案例都是定义两个特定类型的变量,然后查看它的内存地址,然后进行调用一个方法进行相加运算,然后分别在方法内输出变量和结查内存地址,最后返回主方法后变量的内存地址。

值类型:

static void TestDouble()
{
var v1 = 1.00001d;
var v2 = 2.00002d;
Console.WriteLine("TestDouble v1 " + (long)&v1);
Console.WriteLine("TestDouble v2 " + (long)&v2);
Console.WriteLine("TestDouble v2-v1 " + ((long)&v2 - (long)&v1));
var v3 = Add(v1, v2);
Console.WriteLine("TestDouble v3 " + (long)&v3);
Console.WriteLine("TestDouble v3-v2 " + ((long)&v3 - (long)&v2));
Console.WriteLine("TestDouble v3-v1 " + ((long)&v3 - (long)&v1));
}

static double Add(double v1, double v2)
{
Console.WriteLine("Add v1 " + (long)&v1);
Console.WriteLine("Add v2 " + (long)&v2);
Console.WriteLine("Add v2-v1 " + ((long)&v2 - (long)&v1));
var v3 = v1 + v2;
Console.WriteLine("Add v3 " + (long)&v3);
Console.WriteLine("Add v3-v2 " + ((long)&v3 - (long)&v2));
Console.WriteLine("Add v3-v1 " + ((long)&v3 - (long)&v1));
return v3;
}

C#通过不安全代码看内存加载_看内存加载

  v1的所在内存地址大于v2,最后运算完的v3是最小的,我们可以想象,v1放在栈的最后面,地址最大,然后放v2,最后放v3。回收时的顺序是反回来的。那么Add方法里,v2地址最大,但比TestDouble都要小,说明进栈要晚一些,接下来是v1进栈,最后是v3进栈,不过TestDouble里的每个变量都相差8,但方法里的就不是了,这是因为方法参数,返回值等信息,还要占一些内存空间。还有TestDouble的v3为什么能和v2相差8?不是有Add方法吗?原因是Add调用完后都出栈了,所以TestDouble的v3和v2是相邻的。

自定义结构体:

struct TestStruct
{
public TestStruct()
{
i = 100;
}
public long i;
}
static void TestTestStruct()
{
var v1 = new TestStruct();
Console.WriteLine("TestStruct原v1对象地址= " + (long)&v1);
var v2 = new TestStruct();
Console.WriteLine("TestStruct原v2对象地址= " + (long)&v2);
Console.WriteLine("TestStruct v2-v1 " + ((long)&v2 - (long)&v1));
var v3 = Add(v1, v2);
Console.WriteLine("TestStruct原v3对象地址= " + (long)&v3);
Console.WriteLine("TestStruct v3-v2 " + ((long)&v3 - (long)&v2));
}
static TestStruct Add(TestStruct v1, TestStruct v2)
{
Console.WriteLine("Add TestStruct v1对象地址= " + (long)&v1);
Console.WriteLine("Add TestStruct v2对象地址= " + (long)&v2);
Console.WriteLine("Add TestStruct v2-v1 " + ((long)&v2 - (long)&v1));
var v3 = new TestStruct();
v3.i = v1.i + v2.i;
Console.WriteLine("Add TestStruct v3对象地址" + (long)&v3);
Console.WriteLine("Add TestStruct v3-v2 " + ((long)&v3 - (long)&v2));
return v3;
}

C#通过不安全代码看内存加载_看内存加载_02

  自定义struct与double类似,本质上double也是用struct定义的。

引用类型string

static void TestString()
{
long ad1, ad2, ad3;
var v1 = "aaaa";
var v2 = "bbbb";
fixed (char* p = v1)
{
ad1 = (long)p;
Console.WriteLine("TestString v1字符串地址= " + (long)p);
}
fixed (char* p = v2)
{
ad2 = (long)p;
Console.WriteLine("TestString v2字符串地址= " + (long)p);
}
Console.WriteLine("TestString v2-v1 " + (ad2 - ad1));
var v3 = Add(v1, v2);
fixed (char* p = v3)
{
ad3 = (long)p;
Console.WriteLine("TestString v3字符串地址= " + (long)p);
}
Console.WriteLine("TestString v3-v2 " + (ad3 - ad2));
}

static string Add(string v1, string v2)
{
long ad1, ad2, ad3;
fixed (char* p = v1)
{
ad1 = (long)p;
Console.WriteLine("Add中v1字符串地址= " + (long)p);
}
fixed (char* p = v2)
{
ad2 = (long)p;
Console.WriteLine("Add中v2字符串地址= " + (long)p);
}
Console.WriteLine("Add中 v2-v1 " + (ad2 - ad1));
var v3 = v1 + v2;
fixed (char* p = v3)
{
ad3 = (long)p;
Console.WriteLine("Add中v3字符串地址= " + (long)p);
}
Console.WriteLine("Add中 v3-v2 " + (ad3 - ad2));
Console.WriteLine("Add中 v3-v1 " + (ad3 - ad1));
return v3;
}
static void TestString2()
{
var v1 = "aaaa";
var v2 = "bbbb";
var h1 = GCHandle.Alloc(v1, GCHandleType.Pinned);
Console.WriteLine("TestString2 v1对象地址= " + (long)h1.AddrOfPinnedObject());
var h2 = GCHandle.Alloc(v2, GCHandleType.Pinned);
Console.WriteLine("TestString2 v2对象地址= " + (long)h2.AddrOfPinnedObject());
Console.WriteLine("TestString2 v2-v1 " + ((long)h2.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
var v3 = Add2(v1, v2);
var h3 = GCHandle.Alloc(v3, GCHandleType.Pinned);
Console.WriteLine("TestString2 v3对象地址= " + (long)h3.AddrOfPinnedObject());
Console.WriteLine("TestString2 v3-v2 " + ((long)h3.AddrOfPinnedObject() - (long)h2.AddrOfPinnedObject()));
}
static string Add2(string v1, string v2)
{
var h1 = GCHandle.Alloc(v1, GCHandleType.Pinned);
Console.WriteLine("Add2中的v1对象地址= " + (long)h1.AddrOfPinnedObject());
var h2 = GCHandle.Alloc(v2, GCHandleType.Pinned);
Console.WriteLine("Add2中的v2对象地址= " + (long)h2.AddrOfPinnedObject());
Console.WriteLine("Add2 v2-v1 " + ((long)h2.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
var v3 = v1 + v2;
var h3 = GCHandle.Alloc(v3, GCHandleType.Pinned);
Console.WriteLine("Add2中的v3对象地址= " + (long)h3.AddrOfPinnedObject());
Console.WriteLine("Add2 v3-v2 " + ((long)h3.AddrOfPinnedObject() - (long)h2.AddrOfPinnedObject()));
Console.WriteLine("Add2 v3-v1 " + ((long)h3.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
return v3;
}

C#通过不安全代码看内存加载_看内存加载_03

  字符串是引用类型,v1比v2内存地址小,进入Add后,v1和v2与传入的地址相同,因为是引用类型,Add方法里的v3接着往大走,并且与返回的v3是一个地址,这些没有问题。

  string用了两种方法,发现两个方式v1都是aaaa,v2都是bbbb,因为字符串有留用性,所以两个方法的v1和v2是一样的;但两种方式调用了Add后,在Add里的v3都是aaaabbbb,都是拼接,但拼出来的字符串的地址不相同,所以这块没有留用。

自定class类型

class TestClass
{
public int i = 100;
}
static void TestTestClass()
{
var v1 = new TestClass();
var h1 = GCHandle.Alloc(v1, GCHandleType.Pinned);
Console.WriteLine("TestTestClass v1对象地址= " + (long)h1.AddrOfPinnedObject());
var v2 = new TestClass();
var h2 = GCHandle.Alloc(v2, GCHandleType.Pinned);
Console.WriteLine("TestTestClass v2对象地址= " + (long)h2.AddrOfPinnedObject());
Console.WriteLine("TestTestClass v2-v1 " + ((long)h2.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
var v3 = Add(v1, v2);
var h3 = GCHandle.Alloc(v3, GCHandleType.Pinned);
Console.WriteLine("TestTestClass 3对象地址= " + (long)h3.AddrOfPinnedObject());
Console.WriteLine("TestTestClass v3-v2 " + ((long)h3.AddrOfPinnedObject() - (long)h2.AddrOfPinnedObject()));
}
static TestClass Add(TestClass v1, TestClass v2)
{
var h1 = GCHandle.Alloc(v1, GCHandleType.Pinned);
Console.WriteLine("Add中的v1对象地址= " + (long)h1.AddrOfPinnedObject());
var h2 = GCHandle.Alloc(v2, GCHandleType.Pinned);
Console.WriteLine("Add中的v2对象地址= " + (long)h2.AddrOfPinnedObject());
Console.WriteLine("Add中 v2-v1 " + ((long)h2.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
var v3 = new TestClass();
v3.i = v1.i + v2.i;
var h3 = GCHandle.Alloc(v3, GCHandleType.Pinned);
Console.WriteLine("Add中的v3对象地址= " + (long)h3.AddrOfPinnedObject());
Console.WriteLine("Add中 v3-v2 " + ((long)h3.AddrOfPinnedObject() - (long)h2.AddrOfPinnedObject()));
Console.WriteLine("Add中 v3-v1 " + ((long)h3.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
return v3;
}

C#通过不安全代码看内存加载_看内存加载_04

  自定义class,每次都是新地址,没有留用性,并且地址都是在增加。

  当然引用类型的地址不是一成不变的,因为有垃圾回放,重新整理的过程,本例用用Pinned的方式固定,不过代码量少的情况也不一定能触发回收。  

  想要更快更方便的了解相关知识,可以关注微信公众号 

C#通过不安全代码看内存加载_看内存加载_05