偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

淺析如何驗(yàn)證fixed關(guān)鍵字效果

開發(fā) 后端
在這里作者設(shè)置了一個(gè)小實(shí)驗(yàn),來(lái)驗(yàn)證fixed關(guān)鍵字效果。希望通過(guò)本文能對(duì)大家有所幫助。

本文主要是一種驗(yàn)證,對(duì)與fixed關(guān)鍵字操作的的驗(yàn)證,希望通過(guò)本文能讓大家對(duì)fixed關(guān)鍵字理解得更深入一些。同時(shí)也會(huì)分析程序員可能出現(xiàn)問(wèn)題的一些壞習(xí)慣,希望對(duì)大家有所幫助。

#T#

之前談到String連接操作的性能,其中會(huì)涉及到unsafe操作,而unsafe操作必然會(huì)涉及到指針,于是fixed關(guān)鍵字也應(yīng)運(yùn)而生。fixed關(guān)鍵字是用來(lái)pin住一個(gè)引用地址的,因?yàn)槲覀冎繡LR的垃圾收集器會(huì)改變某些對(duì)象的地址,因此在改變地址之后指向那些對(duì)象的引用就要隨之改變。這種改變是對(duì)于程序員來(lái)說(shuō)是無(wú)意識(shí)的,因此在指針操作中是不允許的。否則,我們之前已經(jīng)保留下的地址,在GC后就無(wú)法找到我們所需要的對(duì)象?,F(xiàn)在就來(lái)我們就來(lái)做一個(gè)小實(shí)驗(yàn),驗(yàn)證fixed關(guān)鍵字的效果。

當(dāng)然,這個(gè)實(shí)驗(yàn)很簡(jiǎn)單,簡(jiǎn)單地可能會(huì)讓您笑話。首先我們來(lái)準(zhǔn)備一個(gè)SomeClass類:

  1. public class SomeClass  
  2. {  
  3.     public int Field;  

然后準(zhǔn)備一段代碼:

  1. private static unsafe void GCOutOfFixedBlock()  
  2. {  
  3.     var a = new int[100];  
  4.     var c = new SomeClass();  
  5.  
  6.     fixed (int* ptr = &c.Field)  
  7.     {  
  8.         PrintAddress("Before GC", (int)ptr);  
  9.     }  
  10.  
  11.     GC.Collect(2);  
  12.  
  13.     fixed (int* ptr = &c.Field)  
  14.     {  
  15.         PrintAddress("After GC", (int)ptr);  
  16.     }  
  17. }  
  18.  
  19. private static void PrintAddress(string name, int address)  
  20. {  
  21.     Console.Write(name + ": 0x");  
  22.     Console.WriteLine(address.ToString("X"));  

在GCOutOfFixedBlock方法中,我們首先分配一個(gè)長(zhǎng)度為100的int數(shù)組,然后新建一個(gè)SomeClass對(duì)象。新建數(shù)組的目的在于制造“垃圾”,目的是在調(diào)用GC.Collect方法時(shí)改變SomeClass對(duì)象在堆中的位置。由于垃圾回收發(fā)生在fixed代碼塊之外,這樣我們前后兩次打印出的值便是不同的:

Before GC: 0x1A058C0
After GC: 0x1975DF4

值得注意的是,這段代碼必須在Release模式下進(jìn)行編譯,讓CLR執(zhí)行代碼時(shí)進(jìn)行優(yōu)化,這樣CLR便會(huì)在垃圾回收時(shí)發(fā)現(xiàn)a數(shù)組已經(jīng)是垃圾了(因?yàn)楹竺娴拇a不會(huì)用它),于是會(huì)將其回收——否則便無(wú)法看出地址改變的效果來(lái)。那么,我們重寫一段代碼:

  1. private static unsafe void GCInsideFixedBlock()  
  2. {  
  3.     var a = new int[100];  
  4.     var c = new SomeClass();  
  5.  
  6.     fixed (int* ptr = &c.Field)  
  7.     {  
  8.         PrintAddress("Before GC", (int)ptr);  
  9.         GC.Collect(2);  
  10.     }  
  11.  
  12.     fixed (int* ptr = &c.Field)  
  13.     {  
  14.         PrintAddress("After GC", (int)ptr);  
  15.     }  

結(jié)果如下:

Before GC: 0x1B558C0
After GC: 0x1B558C0

由于GC發(fā)生在fixed代碼塊內(nèi)部,因此c對(duì)象被pin在堆上了,于是GC前后c對(duì)象的地址沒(méi)變,這就是fixed的作用。那么,下面這段代碼運(yùn)行結(jié)果是什么呢?

  1. private static unsafe void Mixed()  
  2. {  
  3.     var a = new int[100];  
  4.     var c1 = new SomeClass();  
  5.     var c2 = new SomeClass();  
  6.  
  7.     fixed (int* ptr1 = &c1.Field)  
  8.     {  
  9.         PrintAddress("Before GC", (int)ptr1);  
  10.     }  
  11.  
  12.     fixed (int* ptr2 = &c2.Field)  
  13.     {  
  14.         PrintAddress("Before GC (fixed)", (int)ptr2);  
  15.         GC.Collect(2);  
  16.     }  
  17.  
  18.     fixed (int* ptr1 = &c1.Field)  
  19.     {  
  20.         PrintAddress("After GC", (int)ptr1);  
  21.     }  
  22.  
  23.     fixed (int* ptr2 = &c2.Field)  
  24.     {  
  25.         PrintAddress("After GC (fixed)", (int)ptr2);  
  26.     }  

至于為什么是這個(gè)結(jié)果,那便和CLR實(shí)現(xiàn)方式有關(guān)了。

原文標(biāo)題:驗(yàn)證fixed關(guān)鍵字效果的小實(shí)驗(yàn)

鏈接:http://www.cnblogs.com/JeffreyZhao/archive/2009/11/29/lab-fixed-keyword.html

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2009-08-13 17:44:34

C# using關(guān)鍵字

2009-06-29 12:58:47

This關(guān)鍵字java

2010-01-26 14:35:11

C++關(guān)鍵字

2012-04-23 13:49:55

PHP技術(shù)

2009-11-26 19:24:54

PHP類CMS

2009-08-21 14:58:56

C# this關(guān)鍵字

2013-01-30 10:12:14

Pythonyield

2018-04-20 15:56:09

Pythonglobal關(guān)鍵字

2009-09-17 09:30:00

Linq LET關(guān)鍵字

2022-01-04 16:35:42

C++Protected關(guān)鍵字

2012-03-01 12:50:03

Java

2009-09-02 09:24:03

C# this關(guān)鍵字

2019-12-20 15:19:41

Synchroinze線程安全

2009-12-17 13:57:15

Ruby關(guān)鍵字

2009-08-06 17:52:23

C#增加that關(guān)鍵字

2024-03-15 15:12:27

關(guān)鍵字底層代碼

2011-06-14 13:26:27

volatile

2009-08-21 14:47:59

C# base關(guān)鍵字

2021-08-06 07:51:47

關(guān)鍵字int函數(shù)

2022-01-10 18:11:42

C語(yǔ)言應(yīng)用技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)