Visual Studio 效能祕訣和訣竅Visual Studio performance tips and tricks

Visual Studio 效能建議是要針對記憶體不足的情況,但這極少發生。Visual Studio performance recommendations are intended for low memory situations, which may occur in rare cases. 在這些情況下,您可以最佳化可能不會使用的特定 Visual Studio 功能。In these situations, you can optimize certain Visual Studio features that you may not be using. 下列祕訣不適合作為一般建議。The following tips are not intended as general recommendations.

注意

如果您因為記憶體問題而無法使用此產品,請透過 意見反應工具讓我們知道。If you’re having difficulty using the product because of memory issues, let us know through the feedback tool.

使用 64 位元 OSUse a 64-bit OS

如果您將系統從 32 位元版本的 Windows 升級至 64 位元版本,請將 Visual Studio 可用的虛擬記憶體數量從 2 GB 擴充為 4 GB。If you upgrade your system from a 32-bit version of Windows to a 64-bit version, you expand the amount of virtual memory available to Visual Studio from 2 GB to 4 GB. 這可讓 Visual Studio 處理更大量的工作負載,即使是 32 位元處理序亦然。This enables Visual Studio to handle significantly larger workloads, even though it is 32-bit process.

如需詳細資訊,請參閱記憶體限制Use /LARGEADDRESSAWARE on 64-bit Windows (在 64 位元 Windows 上使用 /LARGEADDRESSAWARE)。For more information, see Memory limits and Use /LARGEADDRESSAWARE on 64-bit Windows.

停用自動檔案還原Disable automatic file restore

Visual Studio 會自動重新開啟在上一個工作階段保持開啟的文件。Visual Studio automatically reopens documents that were left open in the previous session. 這可能會讓載入解決方案的時間延長達 30% 以上,依開啟的專案類型及文件而定。This can prolong the times it takes to load a solution by up to 30% or more, depending on the project type and the documents being opened. Windows Forms 及 XAML 這類設計工具和某些 JavaScript 與 typescript 檔案可能會很慢才開啟。Designers like Windows Forms and XAML, and some JavaScript and typescript files, can be slow to open.

Visual Studio 會在自動文件還原導致解決方案載入時間明顯變慢時,以黃色的列通知您。Visual Studio notifies you in a yellow bar when automatic document restore is causing a solution to load significantly slower. 您可遵循下列步驟停用自動檔案重新開啟:You can disable automatic file reopening by following these steps:

  1. 選取 [工具 > 選項] 以開啟 [選項] 對話方塊。Select Tools > Options to open the Options dialog box.

  2. 在 [專案和方案 > 一般] 頁面上,取消選取 [在解決方案載入時重新開啟檔]。On the Projects and Solution > General page, deselect Reopen documents on solution load.

若您停用自動檔案還原,則可以使用其中一個移至命令,快速瀏覽到您要開啟的檔案:If you disable automatic file restore, a quick way to navigate to files you want to open is by using one of the Go To commands:

  • 若是一般 移至 功能,請選取 [編輯] > [移至] > [移至全部],或按 Ctrl+TFor the general Go To functionality, select Edit > Go To > Go To All, or press Ctrl+T.

  • 使用 [編輯 > 至 > 上一個編輯位置] 或按 Ctrl + Shift + 倒退鍵,跳至方案中的最後一個編輯位置。Jump to the last edit location in a solution using Edit > Go To > Go To Last Edit Location, or by pressing Ctrl+Shift+Backspace.

  • 使用 [移至最近使用的檔案],查看最近在方案中瀏覽的檔案清單。Use Go To Recent File to see a list of recently visited files in a solution. 選取 [編輯 > 至 > 最近使用 的檔案],或按 ctrl + 1ctrl + RSelect Edit > Go To > Go To Recent File, or press Ctrl+1, Ctrl+R.

設定偵錯選項Configure debugging options

如果您在偵錯工作階段期間通常會記憶體不足,則可以進行一或多個組態變更來最佳化效能。If you are typically running low on memory during debugging sessions, you can optimize performance by making one or more configuration changes.

  • 只啟用 My CodeEnable Just My Code

    最簡單的最佳化是啟用 [Just My Code] 功能,這只會載入您專案的符號。The simplest optimization is to enable the Just My Code feature, which only loads symbols for your project. 啟用此功能可以大幅降低用於偵錯 Managed 應用程式 (.NET) 的記憶體。Enabling this feature can result in a significant memory saving for debugging managed applications (.NET). 根據預設,某些專案類型已經啟用此選項。This option is already enabled by default in some project types.

    若要啟用 [Just My Code],請選擇 [工具] > [選項] > [偵錯] > [一般],然後選取 [啟用 Just My Code]。To enable Just My Code, choose Tools > Options > Debugging > General, and then select Enable Just My Code.

  • 指定要載入的符號Specify symbols to load

    若為原生偵錯工具,載入符號檔 (.pdb) 在記憶體資源方面很昂貴。For native debugging, loading symbol files (.pdb) is expensive in terms of memory resources. 您可以設定偵錯工具符號設定來節省記憶體。You can configure your debugger symbol settings to conserve memory. 一般而言,您會將方案設定成只從您的專案載入模組。Typically, you configure the solution to only load modules from your project.

    若要指定符號載入,請選擇 [工具 > 選項 > 調試 > 符號]。To specify symbol loading, choose Tools > Options > Debugging > Symbols.

    設定 [僅限指定的模組] 的選項,而不是 [所有模組],然後指定您要載入的模組。Set the options to Only specified modules instead of All modules and then specify which modules you care to load. 偵錯時,您也可以以滑鼠右鍵按一下 [模組] 視窗中的特定模組,以在符號載入中明確包含模組While debugging, you can also right-click specific modules in the Modules window to explicitly include a module in the symbol load. (在進行調試時開啟視窗,請選擇 [ Debug > Windows > 模組]。 ) (To open the window while debugging, choose Debug > Windows > Modules.)

    如需詳細資訊,請參閱 Understand symbol files (了解符號檔)。For more information, see Understand symbol files.

  • 停用診斷工具Disable Diagnostic Tools

    建議您在使用之後停用 CPU 分析。It is recommended that you disable CPU profiling after use. 這項功能可能會耗用大量資源。This feature can consume large amounts of resources. 啟用 CPU 分析之後,會在後續偵錯工作階段之間持續保存此狀態,因此完成時適合明確地關閉它。Once CPU profiling is enabled, this state is persisted across subsequent debug sessions, so it’s worth explicitly turning it off when done. 如果您不需要提供的功能,則在偵錯時停用診斷工具,即可節省一些資源。You may save some resources by disabling the diagnostic tools while debugging if you do not need the provided features.

    若要停用 診斷工具,請啟動偵錯工具,選取 [工具 > 選項] > > [一般],然後取消選取 [在 偵錯工具時啟用診斷工具] 選項。To disable the Diagnostic Tools, start a debugging session, select Tools > Options > Debugging > General, and then deselect the Enable Diagnostic Tools while debugging option.

    如需詳細資訊,請參閱 Profiling Tools (分析工具)。For more information, see Profiling Tools.

停用工具和延伸模組Disable tools and extensions

若要改善效能,可以關閉一些工具或延伸模組。Some tools or extensions can be turned off to improve performance.

提示

一次關閉一個延伸模組,然後重新檢查效能,通常可以隔離效能問題。You can often isolate performance issues by turning off extensions one at a time and rechecking performance.

受控語言服務 (Roslyn)Managed language service (Roslyn)

如需 .NET Compiler Platform ("Roslyn") 效能考量的資訊,請參閱大型解決方案的效能考量For information about .NET Compiler Platform ("Roslyn") performance considerations, see Performance considerations for large solutions.

  • 停用完整解決方案分析Disable full solution analysis

    在叫用組建之前,Visual Studio 會對整個解決方案執行分析,以提供錯誤的豐富體驗。Visual Studio performs analysis on your entire solution in order to provide a rich experience about errors before invoking a build. 這項功能十分適用於盡快識別錯誤。This feature is useful to identify errors as soon as possible. 不過,對於大型解決方案,這項功能可能會耗用大量記憶體資源。However, for large solutions, this feature can consume significant memory resources. 如果您遇到記憶體壓力或類似問題,則可以停用此體驗,來釋出這些資源。If you’re experiencing memory pressure or similar issues, you can disable this experience to free up these resources. 根據預設,會針對 Visual Basic 啟用這個選項,但針對 C# 則會予以停用。By default, this option is enabled for Visual Basic and disabled for C#.

    若要停用 [完整解決方案分析],請選擇 [工具] > [選項] > [文字編輯器],然後選取 [Visual Basic] 或 [C#]。To disable Full Solution Analysis, choose Tools > Options > Text Editor, then select either Visual Basic or C#. 選擇 [進階],然後取消選取 [啟用完整解決方案分析]。Choose Advanced and deselect Enable full solution analysis.

  • 停用 CodeLensDisable CodeLens

    Visual Studio 會對顯示的每個方法執行 [尋找所有參考] 工作。Visual Studio performs a Find All References task on each method as it is displayed. CodeLens 會提供參考數目內嵌顯示這類功能。CodeLens provides features such as the inline display of the number of references. 工作會在不同的處理序中執行,例如 ServiceHub.RoslynCodeAnalysisService32The work is performed in a separate process such as ServiceHub.RoslynCodeAnalysisService32. 在大型解決方案或資源受限的系統中,這項功能會對效能有顯著影響。In large solutions, or on resource-constrained systems, this feature can have a significant impact on performance. 如果您遇到記憶體問題 (例如在 4 GB 電腦上載入大型解決方案時) 或此處裡序使用大量 CPU,可以停用 CodeLens 來釋出資源。If you’re experiencing memory issues, for example, when loading a large solution on a 4-GB machine, or high CPU usage for this process, you can disable CodeLens to free up resources.

    若要停用 CodeLens,請選擇 [工具] > [選項] > [文字編輯器] > [所有語言] > [CodeLens],然後取消選取這項功能。To disable CodeLens, choose Tools > Options > Text Editor > All Languages > CodeLens, and deselect the feature.

    注意

    CodeLens 適用於 Visual Studio 的 Professional 和 Enterprise 版本。CodeLens is available in the Professional and Enterprise editions of Visual Studio.

其他工具和延伸模組Other tools and extensions

  • 停用擴充功能Disable Extensions

    延伸模組是新增至 Visual Studio 的額外軟體元件,可提供新功能或延伸現有功能。Extensions are additional software components added to Visual Studio that provide new functionality or extend existing functionality. 延伸模組通常可能是記憶體資源問題的來源。Extensions can often be a source of memory resource issues. 如果您遇到記憶體資源問題,請嘗試一次停用一個延伸模組,以查看它對案例或工作流程的影響。If you’re experiencing memory resource problems, try disabling extensions one at a time to see how it impacts the scenario or workflow.

    若要停用擴充功能,移至 [工具][擴充功能和更新] > ,然後停用特定擴充功能。To disable extensions, go to Tools > Extensions and Updates, and disable a particular extension.

    若要停用擴充功能,移至 [擴充功能][管理擴充功能] > ,然後停用特定擴充功能。To disable extensions, go to Extensions > Manage Extensions, and disable a particular extension.

  • 停用地圖模式Disable map mode

    地圖模式 會以縮圖顯示捲軸上的程式程式碼。Map mode displays lines of code, in miniature, on the scroll bar. 預設會啟用地圖模式。Map mode is enabled by default.

    若要停用地圖模式,請移至 [工具 > 選項 > 文字編輯器 > 所有語言 > 捲軸],然後在 [行為] 區段中,取消選取 [使用垂直捲動條的地圖模式] 選項。To disable map mode, go to Tools > Options > Text Editor > All Languages > Scroll Bars, and in the Behavior section, deselect the Use map mode for vertical scroll bar option.

  • 停用自動換行Disable word wrap

    [自動換行] 會顯示長行程式碼中,延伸超過 [程式碼編輯器] 視窗目前寬度的部分。Word wrap displays the portion of a long line of code that extends beyond the current width of the code editor window. 預設會開啟自動換行。Word wrap is on by default.

    若要針對目前正在處理的專案停用自動換行,請移至 [編輯 > 自動換 > ]。To disable word wrap for a project that you are currently working on, go to Edit > Advanced > Word Wrap. (您可以使用相同的功能表命令來切換此設定。 ) (You can toggle this setting by using the same menu commands.)

    若要停用所有專案的自動換行,請移至 [工具 > 選項 > 一般 > 文字編輯器 > 所有語言], > 然後在 [設定] 區段中,取消選取 [自動換行] 選項。To disable word wrap for all projects, go to Tools > Options > General > Text Editor > All Languages > General, and in the Settings section, deselect the Word wrap option.

  • 停用 XAML 設計工具Disable XAML Designer

    預設會啟用 XAML 設計工具,但只有在您開啟 .xaml 檔案時,才會耗用資源。The XAML designer is enabled by default, but only consumes resources if you open a .xaml file. 如果您使用 XAML 檔案,但不想要使用設計工具功能,請停用這項功能,以釋出一些記憶體。If you work with XAML files but do not wish to use the designer functionality, disable this feature to free up some memory.

    若要停用 xaml 設計工具,請移至 [工具 > 選項 > xaml 設計 工具 > 啟用 xaml 設計 工具],然後取消選取此選項。To disable XAML Designer, go to Tools > Options > XAML Designer > Enable XAML Designer, and deselect the option.

  • 移除工作負載Remove workloads

    您可以使用 Visual Studio 安裝程式以移除不再使用的工作負載。You can use the Visual Studio Installer to remove workloads that are no longer used. 這個動作可以略過不再需要的套件和組件,來簡化啟動和執行階段成本。This action can streamline the startup and runtime cost by skipping packages and assemblies that aren’t needed anymore.

  • 將未追蹤的檔案新增至本機 .gitignoreAdd untracked files to local .gitignore

    Visual Studio 會使用未追蹤的檔案執行 Git 命令, git status 以在您將新檔案新增至存放庫時提供順暢的體驗。Visual Studio runs the Git command git status with untracked files to provide a seamless experience when you add new files to a repository. 當有大量未追蹤的檔案時, git status 可能會耗用額外的記憶體。When there are a large number of untracked files, git status can consume extra memory. 若要忽略這些檔案並改善的效能 git status ,您可以將這些檔案或資料夾新增至您的本機 .gitignore 檔案。To ignore these files and improve performance of git status, you can add these files or folders to your local .gitignore file. 若要存取檔案,請移至 git > 設定 > git 存放庫設定To access the file, go to Git > Settings > Git Repository Settings. 然後,在 [ Git 檔案] 區段中,按一下 [ 新增 ] 建立 .gitignore 檔案,或按一下 [ 編輯 ] (如果您已經有的話)。Then, in the Git files section, click Add to create a .gitignore file, or click Edit if you already have one.

強制執行記憶體回收Force a garbage collection

CLR 使用記憶體回收記憶體管理系統。The CLR uses a garbage collection memory management system. 在此系統中,有時不再需要的物件會使用記憶體。In this system, sometimes memory is used by objects that are no longer needed. 這種狀態是暫時的;記憶體回收行程會根據其效能和資源使用量啟發學習法來釋放這個記憶體。This state is temporary; the garbage collector will release this memory based on its performance and resource usage heuristics. 您可以使用 Visual Studio 中的快速鍵,強制 CLR 收集任何未使用的記憶體。You can force the CLR to collect any unused memory by using a hotkey in Visual Studio. 如果有大量的垃圾等候回收,而您強制進行垃圾收集,您應該會在 [工作管理員] 中看到 devenv.exe 進程的記憶體使用量。If there is a significant amount of garbage waiting for collection and you force a garbage collection, you should see the memory usage of the devenv.exe process drop in Task Manager. 很少需要使用這種方法。It’s rarely necessary to use this method. 不過,完成耗費資源的作業 (例如完整建置、偵錯工作階段或方案開啟事件) 之後,即可協助您判斷處理序實際正在使用的記憶體數量。However, after an expensive operation has completed (such as a full build, debug session, or a solution open event), it can help you determine how much memory is really being used by the process. 因為 Visual Studio 是混合的 (Managed 和原生),所以原生配置器和記憶體回收行程可能偶而會競爭有限的記憶體資源。Because Visual Studio is mixed (managed & native), it’s occasionally possible for the native allocator and the garbage collector to compete for limited memory resources. 在高記憶體使用量的情況下,它有助於強制執行記憶體回收行程。Under conditions of high memory usage, it may help to force the garbage collector to run.

若要強制垃圾收集,請使用快速鍵: ctrl + alt + shift + f12ctrl + alt + shift + f12 (按兩次) 。To force a garbage collection, use the hotkey: Ctrl+Alt+Shift+F12, Ctrl+Alt+Shift+F12 (press it twice).

如果可靠地強制執行記憶體回收可讓您的案例運作,請透過 Visual Studio 意見反應工具提出報表,因為這種行為可能是 Bug。If forcing garbage collection reliably makes your scenario work, file a report through the Visual Studio feedback tool as this behavior is likely to be a bug.

如需 CLR 記憶體回收行程的詳細描述,請參閱記憶體回收的基本概念For a detailed description of the CLR garbage collector, see Fundamentals of garbage collection.

另請參閱See also