使用 Visual Studio 偵錯工具中的記憶體視窗 (c #、c + +、Visual Basic、F # ) Use the Memory windows in the Visual Studio debugger (C#, C++, Visual Basic, F#)

在偵錯工具期間,[ 記憶體 ] 視窗會顯示您的應用程式所使用的記憶體空間。During debugging, the Memory window shows the memory space your app is using.

[監看式]、[自動變數 ]、[****區域變數] 和 [快速****監看 式] 對話方塊之類的偵錯工具視窗會顯示變數,這些變數會儲存在記憶體Debugger windows like Watch, Autos, Locals, and the QuickWatch dialog show you variables, which are stored at specific locations in memory. [ 記憶體 ] 視窗會顯示整體的圖片。The Memory window shows you the overall picture. 記憶體視圖對於檢查大量資料 (緩衝區或大型字串很方便,例如,) 不會在其他視窗中正確顯示。The memory view is convenient for examining large pieces of data (buffers or large strings, for example) that don't display well in the other windows.

記憶體 視窗不限於顯示資料。The Memory window isn't limited to displaying data. 它會顯示記憶體空間中的所有內容,包括資料、程式碼,以及未指派記憶體中的隨機垃圾資料位。It displays everything in the memory space, including data, code, and random bits of garbage in unassigned memory.

記憶體 視窗不適用於腳本或 SQL 偵錯工具。The Memory window isn't available for script or SQL debugging. 這些語言無法辨識記憶體的概念。Those languages don't recognize the concept of memory.

開啟記憶體視窗Open a Memory window

記憶體 視窗和其他偵錯工具視窗一樣,只有在進行偵錯工具時才能使用。Like other debugger windows, the Memory windows are available only during a debugging session.

重要

若要啟用 記憶體 視窗,您必須在 [工具 選項] (或 [調試選項] 中選取 [啟用位址層級的調試] > > ) > 的 > [一般] 偵測。To enable the Memory windows, Enable address-level debugging must be selected in Tools > Options (or Debug > Options) > Debugging > General.

若要開啟記憶體視窗To open a Memory window

  1. 確定已在 [工具 選項] (或 [偵錯工具選項] 中選取 [啟用位址層級的調試 程式] > > ) > 的 > [一般]Make sure Enable address-level debugging is selected in Tools > Options (or Debug > Options) > Debugging > General.

  2. 選取綠色箭號、按 F5,或選取 [ Debug > 開始調試 程式],開始進行偵錯工具。Start debugging by selecting the green arrow, pressing F5, or selecting Debug > Start Debugging.

  3. [調試 時間] 下 > > ****,選取 [記憶體 1]、[記憶體 2]、[記憶體 3] 或 [記憶體 4]。Under Debug > Windows > Memory, select Memory 1, Memory 2, Memory 3, or Memory 4. (部分版本 Visual StudioVisual Studio 僅提供一個 記憶體 視窗。 ) (Some editions of Visual StudioVisual Studio offer only one Memory window.)

在 [記憶體] 視窗中四處移動Move around in the Memory window

電腦的位址空間很大,而且您可以在 [ 記憶體 ] 視窗中,輕鬆地遺失您的地點。The address space of a computer is large, and you can easily lose your place by scrolling in the Memory window.

較高的記憶體位址會出現在視窗底部。Higher memory addresses appear at the bottom of the window. 若要查看較高的位址,請向下移動。To view a higher address, scroll down. 若要查看較低的位址,請向上移動。To view a lower address, scroll up.

您可以使用拖放功能,或在 [位址] 欄位中輸入位址,立即移至 [記憶體] 視窗中指定的位址。You can instantly go to a specified address in the Memory window by using drag-and-drop, or by entering the address in the Address field. [ 位址 ] 欄位可接受英數位元位址,以及評估為位址的運算式,例如 e.User.NonroamableIdThe Address field accepts alphanumeric addresses, and expressions that evaluate to addresses, such as e.User.NonroamableId.

若要強制立即重新評估 [ 位址 ] 欄位中的運算式,請選取 [自動重新 評估 ] 圖示。To force immediate re-evaluation of an expression in the Address field, select the rounded-arrow Reevaluate Automatically icon.

依預設,[ 記憶體 ] 視窗會將 位址 運算式視為即時運算式,這些運算式會在應用程式執行時重新評估。By default, the Memory window treats Address expressions as live expressions, which are re-evaluated as the app runs. 例如,即時運算式可能很有用,例如,用來查看指標變數所觸及的記憶體。Live expressions can be useful, for example, to view the memory that is touched by a pointer variable.

使用拖放來移至記憶體位置:To use drag and drop to move to a memory location:

  1. 在任何偵錯工具視窗中,選取記憶體位址,或包含記憶體位址的指標變數。In any debugger window, select a memory address, or a pointer variable that contains a memory address.

  2. 將位址或指標拖放到 [ 記憶體 ] 視窗中。Drag and drop the address or pointer in the Memory window. 該位址接著會出現在 [ 位址 ] 欄位中,而 [ 記憶體 ] 視窗則會調整,以在頂端顯示該位址。That address then appears in the Address field, and the Memory window adjusts to display that address at the top.

若要移至記憶體位置,請在 [位址] 欄位中輸入它:To move to a memory location by entering it in the Address field:

  • 在 [ 位址 ] 欄位中輸入或貼上位址或運算式,然後按 enter 鍵,或從 [ 位址 ] 欄位的下拉式清單中選擇它。Type or paste the address or expression in the Address field and press Enter, or choose it from the dropdown in the Address field. [ 記憶體 ] 視窗會調整,以在頂端顯示該位址。The Memory window adjusts to display that address at the top.

自訂記憶體視窗Customize the Memory window

依預設,記憶體內容會顯示為十六進位格式的1位元組整數,而視窗寬度則會決定所顯示的資料行數目。By default, memory contents appear as 1-byte integers in hexadecimal format, and the window width determines the number of columns shown. 您可以自訂 [記憶體] 視窗顯示記憶體內容的方式。You can customize the way the Memory window shows memory contents.

若要變更記憶體內容的格式:To change the format of the memory contents:

  • 在 [ 記憶體 ] 視窗中按一下滑鼠右鍵,然後從操作功能表中選擇您要的格式。Right-click in the Memory window, and choose the formats that you want from the context menu.

若要變更記憶體視窗中的資料行數目:To change the number of columns in the Memory window:

  • 選取 [資料 ] 欄位旁的下拉式箭號,並選取要顯示的資料行數目,或選取 [ 自動 ] 以根據視窗寬度進行自動調整。Select the drop down arrow next to the Columns field, and select the number of columns to display, or select Auto for automatic adjustment based on window width.

如果您不想要在應用程式執行時變更 記憶體 視窗的內容,您可以關閉即時運算式評估。If you do not want the contents of the Memory window to change as your app runs, you can turn off live expression evaluation.

若要切換即時評估:To toggle live evaluation:

  • 在 [ 記憶體 ] 視窗中按一下滑鼠右鍵,然後在內容功能表中選取 [ 自動重新評估 ]。Right-click in the Memory window, and select Reevaluate Automatically in the context menu.

    注意

    即時運算式評估為切換,預設為開啟,因此選取 [重新 評估] 會自動 關閉。Live expression evaluation is a toggle, and is on by default, so selecting Reevaluate Automatically turns it off. 選取 [重新 評估] 會自動 重新開啟。Selecting Reevaluate Automatically again turns it back on.

您可以在 [記憶體] 視窗的頂端隱藏或顯示工具列。You can hide or display the toolbar at the top of the Memory window. 隱藏工具列時,您將無法存取 [ 位址 ] 欄位或其他工具。You will not have access to the Address field or other tools when the toolbar is hidden.

若要切換工具列顯示:To toggle the toolbar display:

  • 在 [ 記憶體 ] 視窗上按一下滑鼠右鍵,然後在內容功能表中選取 [ 顯示工具列 ]。Right-click in the Memory window, and select Show Toolbar in the context menu. 工具列會不會出現,需視先前的狀態而定。The toolbar appears or disappears, depending on its previous state.

遵循記憶體的指標Follow a pointer through memory

在原生程式碼應用程式中,您可以使用暫存器名稱作為即時運算式。In native code apps, you can use register names as live expressions. 例如,您可使用堆疊指標隨著堆疊而變動位置。For example, you can use the stack pointer to follow the stack.

若要在記憶體後面跟隨指標:To follow a pointer through memory:

  1. 在 [ 記憶體 視窗 位址 ] 欄位中,輸入目前範圍中的指標運算式。In the Memory window Address field, enter a pointer expression that is in the current scope. 有時您可能會需要取值 (Dereference),視語言而定。Depending on the language, you might have to dereference it.

  2. EnterPress Enter.

    當您使用 debug 命令(例如 Step)時,[ 位址 ] 欄位和 [ 記憶體 ] 視窗頂端顯示的記憶體位址會在指標變更時自動變更。When you use a debug command such as Step, the memory address displayed in the Address field and at the top of the Memory window automatically changes as the pointer changes.

另請參閱See also