You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: OutOfMemoryException Exception when you execute a query
3
3
description: This article provides workarounds for the problem that occurs when you use SQL Server Management Studio (SSMS) to run a SQL query that returns a large amount of data.
4
-
ms.date: 10/29/2020
4
+
ms.date: 04/02/2025
5
5
ms.custom: sap:SQL Server Management, Query and Data Tools
6
6
---
7
-
# Exception when you execute a query in SQL Server Management Studio
7
+
# OutOfMemoryException Exception when you execute a query in SQL Server Management Studio (SSMS)
8
8
9
9
This article helps you resolve the problem that occurs when you use SQL Server Management Studio (SSMS) to run a SQL query that returns a large amount of data.
When you use SSMS to run a SQL query that returns a large amount of data, you receive an error message that resembles the following:
16
+
When you use SSMS to run a SQL query that returns a very large amount of data, you receive an error message that resembles the following:
17
17
18
18
> An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown
19
19
20
20
## Cause
21
21
22
-
This issue occurs because SSMS has insufficient memory to allocate for large results.
23
-
24
-
> [!NOTE]
25
-
> SSMS is a 32-bit process. Therefore, it is limited to 2 GB of memory. SSMS imposes an artificial limit on how much text that can be displayed per database field in the results window. This limit is 64 KB in "Grid" mode and 8 KB in Text mode. If the result set is too large, the memory that is required to display the query results may surpass the 2 GB limit of the SSMS process. Therefore, a large result set can cause the error that is mentioned in the [Symptoms](#symptoms) section.
22
+
This issue occurs because SSMS has insufficient memory to allocate for large results. SSMS is a 32-bit process. Therefore, it is limited to 2 GB of user-mode virtual memory. SSMS imposes an artificial limit on how much text that can be displayed per database field in the results window. This limit is 64 KB in "Grid" mode and 8 KB in Text mode. If the result set is too large, the memory that is required to display the query results may surpass the 2 GB limit of the SSMS process. Therefore, a large result set can cause the error that is mentioned in the [Symptoms](#symptoms) section.
26
23
27
24
## Workaround
28
25
@@ -49,29 +46,4 @@ Configure the query window to output the query results to a file. A file output
49
46
50
47
Use [sqlcmd Utility](/sql/tools/sqlcmd-utility?redirectedfrom=MSDN&view=sql-server-ver15&preserve-view=true) instead of SSMS to run the SQL queries. This method enables queries to be run without the resources that are required by the SSMS UI. Additionally, you can use the 64-bit version of Sqlcmd.exe to avoid the memory restriction that affects the 32-bit SSMS process.
0 commit comments