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 6091167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:17:47+00:00 2026-05-23T12:17:47+00:00

I keep getting System.OutOfMemory exception in my WinForms App which is thrown while doing

  • 0

I keep getting System.OutOfMemory exception in my WinForms App which is thrown while doing an SQL Update I guess. How can I fix that? Here is the case, I am using SQLEXPRESS, trying the code on a 32-bit machine so ThreadPool has default 1023 avaliable worker threads on startup.

MyClass myClass = new MyClass();

for (int i = 0; i < 1000000; i++)
{              
    ThreadPool.QueueUserWorkItem(new WaitCallback(Foo), myClass);
}

private void Foo(object state)
{
    //Some Stuff and SQL UPDATE
}

When the application starts, the ThreadPool starts with 8 threads and starts to increase allocated thread number to perform the job. A while later, it comes to 200 threads for example and can handle it no more, gives out System.OutOfMemory exception. When I check the stack trace, i can see that exception occurs in SQL operation of the method. What should I do? Do I need to increase Buffer Size of my database? I do not want to limit ThreadPools max size by the way or try Thread.Sleep() to have slower, less frequent requests to DB.

  • 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. Editorial Team
    Editorial Team
    2026-05-23T12:17:47+00:00Added an answer on May 23, 2026 at 12:17 pm

    Blindly creating lots of threads to make an operation go faster is not a good solution and almost never works (except by luck maybe). Keep in mind that each threads gets its own stack allocated which is 1MB by default I think. So in case you get 1000 threads going that will use 1GB of RAM just like that.

    If you hit the DB then the work you can do in parallel will be very likely limited by disk I/O so throwing more threads at it might actually make it worse.

    Also be aware that asynchroneous operations are executed on threadpool threads and if you tie them all up with your own work you may run into the problem to starve those operations (meaning they might be never or very late executed). The threadpool is designed to run only short lived tasks. If you need long running tasks then use a different threadpool (for example the SmartThreadPool) or create you own set of threads to handle the work.

    Depending on what your SQL operations are you might hit the Large Object Heap fragmentation problem. Objects larger than 85.000 bytes are put on the LOH which is not being compacted and you can run into unexpected OOM ecceptions. So check if you are creating large arrays or lists of objects.

    Otherwise: Use the debugging tools for windows to make a memory dump and look at what objects you have settings around eating all the memory and what holds the references to keep them alive. Alternatively you could use a .NET memory profiler but most of the really usefull ones are not free (however they usually come with a X days evaluation period).

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

Sidebar

Related Questions

I keep getting a 'System.OutOfMemoryException' thrown at the code below. I can't figure out
i keep getting this error: Exception Details: System.NotSupportedException: Cannot serialize member HannaPrintsDataAccess.Customer.CustomerAddresses of type
I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I keep getting stuck conceptually on deciding an Exception-handling structure for my project. Suppose
I keep getting this NPE in my application and I can't seem to get
I keep getting this error System.Web.HttpException was unhandled by user code Message=Validation of viewstate
I keep getting an error of: Unhandled exception at 0x5a6fca58 (msvcr100d.dll) in Gofish.exe: 0xC0000005:
I keep getting a ClassCastException Exception in thread main java.lang.ClassCastException: java.lang.String cannot be cast
I keep getting compiler errors when I try to access flashVars in an AS3
I keep getting asked about AppDomains in interviews, and I know the basics :

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.