1. 程式人生 > 其它 >C#通過不安全程式碼看記憶體載入

C#通過不安全程式碼看記憶體載入

(注:本篇用點長,有點繞,耐心瀏覽)

  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;
}

  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;
}

  自定義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;
}

  字串是引用型別,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;
}

  自定義class,每次都是新地址,沒有留用性,並且地址都是在增加。

  當然引用型別的地址不是一成不變的,因為有垃圾回放,重新整理的過程,本例用用Pinned的方式固定,不過程式碼量少的情況也不一定能觸發回收。  

  想要更快更方便的瞭解相關知識,可以關注微信公眾號