2014-04-17 59 views
3

據我所知,如果INT變量(值類型)是引用類型)內直接聲明,用於在分配的變量的內存。如何知道內存分配給引用和值類型?

但是,如果在類中有一個方法並且該變量是在一個方法內聲明的,或者它是一個參數,則分配在堆棧上的內存

public class A 
{ 
    int x; // heap 

    public void Func(int y) // stack 
    { 
     int z; // stack 
    } 
} 

如何查看內存分配的位置?

+1

請參閱此[參考鏈接](http://msdn.microsoft.com/en-us/library/0xy59wtx(v = vs.110).aspx「垃圾收集」),它完全關於內存管理通過在asp.net框架中進行垃圾回收。 –

回答

4

當你說'內存位於何處'時,我假設你指的是給變量 提供的實際虛擬地址,也可能是它駐留在專用字節中的已提交塊或區域。 雖然這在平凡的最好的,因爲它可以繞 移動(這就是爲什麼本地互操作開發人員必須使用內存之前PIN), 你可以追捕類似於以下變量的位置:

鑑於源(和斷點):

Source

允許拆卸(選項 - >調試 - >常規 - >顯示拆卸...) 右鍵單擊(上下文菜單):選擇 '轉到拆卸'

enter image description here

注z的賦值:

mov dword ptr [ebp-44h],eax 

打開寄存器窗口(調試 - >視窗 - >寄存器)。 請注意EBP(基址指針)== 0x05C0EF18的值。

Registers

使用計算值(程序員模式),以從上述確定[EBP-44H]。 0x05C0EF18 - 0×44 == 0x05C0EED4

採取在該存儲器位置偷看(調試 - >視窗 - >內存 - >存儲器1) 粘貼在結果值(0x05C0EED4此實例)。存儲在它

memory

注意價值

87 d6 12 00 // <-- Little Endian (00 12 d6 87 <-- Big Endian) 
      //  0x0012D687 (hex) == 1234567 (decimal) 

我們可以確定(現在)是Z位於0x05C0EED4 並具有1234567(十進制)的位置。

然而,x有點複雜。 x位於EAX + 4。

// Similar exercise as above... 
// values get moved into registers, then the assignment... 
mov eax,dword ptr [ebp-3Ch] // [ebp-3Ch] == 0x05C0EF18 - 0x3C == 0x05C0EEDC 
          // Move the value at 0x05C0EEDC (7c d4 40 02) to EAX 
          // [7c d4 40 02] (Big Endian) --> [02 40 d4 7c] (Little Endian) 
          // [02 40 d4 7c] (Little Endian) == 0x0240d47c or 37803132 (decimal) 

mov edx,dword ptr [ebp-40h] // [ebp-40h] == 0x05C0EF18 - 0x40 == 0x05C0EED8 
          // Move the value at 0x05C0EED8 (87 d6 12 00) to EDX 
          // [87 d6 12 00] (Big Endian) --> [00 12 d6 87] (Little Endian) 
          // [00 12 d6 87] (Little Endian) == 0x0012d687 or 1234567 (decimal) 

mov dword ptr [eax+4],edx // [EAX == 0x0240d47c] + 4 = 0x240D480 
          // Move the value at EDX (0x0012d687 or 1234567 (decimal)) to 
          // EAX+4 (x variable located at 0x240D480) 
          //  (currently holds [b1 cb 74 00] (Big Endian) 
          //  which is equal to 7654321 (decimal) 

memory memory memory

然而,也有趣的是虛擬內存映射。使用sysinternals中的vmmmap.exe可以更好地瞭解保留頁面和已提交頁面。然後就可以在不同的世代戳在GC等

Virtual Memory

希望這有助於!

+0

謝謝,這是一個很好的答案=) – dima

相關問題