System.OutOfMemoryException for Management Studio

I have seen multiple posts on the web trying to decipher the OutOfMemoryException thrown by Management Studio (SSMS) while executing a user query. Contrary to popular beliefs, this is not a SQL Database Engine Out of Memory condition. This is a client side OOM condition thrown by the .NET runtime while trying to receive the output of the query from the Database Engine. More often than not, the culprit is a large number of rows returned by the query submitted by the user.

A lot of times the error can be circumvented by using the TEXT mode output of SSMS. The error is a .NET Out of Memory exception pointing to the Management Studio running out of available physical memory. The GRID output requires a .NET GridView control to be created where as the Text mode output uses a TextBox to store the output returned by the database engine. The amount of memory consumed by the GRID is higher than a TextBox.

It is always advisable to store the output of a query returns a large number of rows into a file (CTRL+SHIFT+F) or use SQLCMD to generate the output into a CSV/TXT file. This would help optimize the memory usage on the box that is executing the query and also prevent re-execution of the query due to client box out-of-memory conditions. IMHO I cannot fathom the need to output a million rows in the GRID view because it is not possible to parse the output unless you put that into a flat file! Hope this sheds some light on this common misconception.

Once such issue is mentioned below:

Reference: OOM error when we access Schema changes report from SSMS – Microsoft

Comments

6 responses to “System.OutOfMemoryException for Management Studio”

  1. Faiz Avatar

    what if I am getting the same error after applying a “top” ?

    Like

  2. […] This post was Twitted by banerjeeamit […]

    Like

  3. Common SQL Server myths – Series II – SQL Memory « TroubleshootingSQL Avatar

    […] System.OutOfMemoryException encountered while executing a query from SSMS is not a Database Engine Out-of-Memory condition I have seen multiple suggestions on forums for reducing Max Server Memory. This error is a .NET exception telling the user that an OOM condition occurred. The database engine is not a managed application. It was coded in native language and hence, wouldn’t throw a .NET OOM error. I have written about this in an earlier blog post: System.OutOfMemoryException for Management Studio […]

    Like

  4. Amit Banerjee Avatar

    How many rows were you trying to fetch using TOP? Try using sqlcmd or Results to File option in SSMS.

    Like

  5. Alastair Avatar
    Alastair

    Excellent. Addressed my issue perfectly.

    Like

  6. […] an another post about SQL OutOfMemoryException […]

    Like