Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 92833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:14:58+00:00 2026-05-10T23:14:58+00:00

I have a dynamic query that returns around 590,000 records. It runs successfully the

  • 0

I have a dynamic query that returns around 590,000 records. It runs successfully the first time, but if I run it again, I keep getting a System.OutOfMemoryException. What are some reasons this could be happening?

The error is happening here:

  public static DataSet GetDataSet(string databaseName,string                                    storedProcedureName,params object[] parameters)     {         //Creates blank dataset         DataSet ds = null;          try         {             //Creates database             Database db = DatabaseFactory.CreateDatabase(databaseName);             //Creates command to execute             DbCommand dbCommand = db.GetStoredProcCommand(storedProcedureName);             dbCommand.CommandTimeout = COMMAND_TIMEOUT;             //Returns the list of SQL parameters associated with that stored proecdure             db.DiscoverParameters(dbCommand);              int i = 1;             //Loop through the list of parameters and set the values             foreach (object parameter in parameters)             {                 dbCommand.Parameters[i++].Value = parameter;             }             //Retrieve dataset and set to ds             ds = db.ExecuteDataSet(dbCommand);         }             //Check for exceptions         catch (SqlException sqle)         {             throw sqle;         }         catch (Exception e)         {             throw e; // Error is thrown here.         }         //Returns dataset         return ds;     } 

Here is the code the runs on the button click:

protected void btnSearchSBIDatabase_Click(object sender, EventArgs e) {          LicenseSearch ls = new LicenseSearch();          DataTable dtSearchResults = new DataTable();          dtSearchResults = ls.Search();          Session['dtSearchResults'] = dtSearchResults;          Response.Redirect('~/FCCSearch/SearchResults.aspx');         }         else             lblResults.Visible = true;     } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:14:58+00:00Added an answer on May 10, 2026 at 11:14 pm

    It runs successfully the first time, but if I run it again, I keep getting a System.OutOfMemoryException. What are some reasons this could be happening?

    Regardless of what the others have said, the error has nothing to do with forgetting to dispose your DBCommand or DBConnection, and you will not fix your error by disposing of either of them.

    The error has everything to do with your dataset which contains nearly 600,000 rows of data. Apparently your dataset consumes more than 50% of the available memory on your machine. Clearly, you’ll run out of memory when you return another dataset of the same size before the first one has been garbage collected. Simple as that.

    You can remedy this problem in a few ways:

    • Consider returning fewer records. I personally can’t imagine a time when returning 600K records has ever been useful to a user. To minimize the records returned, try:

      • Limiting your query to the first 1000 records. If there are more than 1000 results returned from the query, inform the user to narrow their search results.

      • If your users really insist on seeing that much data at once, try paging the data. Remember: Google never shows you all 22 bajillion results of a search at once, it shows you 20 or so records at a time. Google probably doesn’t hold all 22 bajillion results in memory at once, it probably finds its more memory efficient to requery its database to generate a new page.

    • If you just need to iterate through the data and you don’t need random access, try returning a datareader instead. A datareader only loads one record into memory at a time.

    If none of those are an option, then you need to force .NET to free up the memory used by the dataset before calling your method using one of these methods:

    • Remove all references to your old dataset. Anything holding on to a refenence of your dataset will prevent it from being reclaimed by memory.

    • If you can’t null all the references to your dataset, clear all of the rows from the dataset and any objects bound to those rows instead. This removes references to the datarows and allows them to be eaten by the garbage collector.

    I don’t believe you’ll need to call GC.Collect() to force a gen cycle. Not only is it generally a bad idea to call GC.Collect(), because sufficient memory pressure will cause .NET invoke the garbage collector on its own.

    Note: calling Dispose on your dataset does not free any memory, nor does it invoke the garbage collector, nor does it remove a reference to your dataset. Dispose is used to clean up unmanaged resources, but the DataSet does not have any unmanaged resources. It only implements IDispoable because it inherents from MarshalByValueComponent, so the Dispose method on the dataset is pretty much useless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It is because of constant class loading. Java stores class… May 11, 2026 at 11:22 am
  • added an answer Sounds like you're probably CPU bound. All the programs you… May 11, 2026 at 11:22 am
  • added an answer Try N0 for no decimal part: string formatted = a.ToString('N0');… May 11, 2026 at 11:22 am

Related Questions

I have a dynamic query that returns around 590,000 records. It runs successfully the
I have a Dynamic Data Entities Web Application that uses a database with GUIDs
I have a dynamic class that serves as a storage container for configuration settings.
How can I have a dynamic variable setting the amount of rows to return
I'm trying to get a dynamic movie clip from the timeline. I have a
I have built a CFC designed to serve as a dynamic, aging cache intended
I have recently written a dynamic querying tool using expression trees and as I
I have a variable of type Dynamic and I know for sure one of
I have a custom built ajax [div] based dynamic dropdown. I have an [input]
I have a few GridItem components that gets filled with dynamic data. Sometimes this

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.