.net 多线程炸栈问题记录

65 阅读1分钟

问题现象

  1. 预:开启线程超过1000多;
  2. 程序运行时抛出异常,且未开启预期1000多线程;
  3. 程序运行时,cpu、内存占用均较小;cpu 30%,内存 300M;
  4. x64程序运行正常,x86程序运行则报 'System.OutOfMemoryException';

错误:

Exception of type 'System.OutOfMemoryException'

启动采集线程时异常 
 System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Threading.Thread.StartInternal(ThreadHandle t, Int32 stackSize, Int32 priority, Char* pThreadName)
   at System.Threading.Thread.StartCore()
   at System.Threading.Thread.Start()
   at ***.StartThread() in ***.cs:line 234
   at ***.<>c__DisplayClass13_1.<CreateDeviceThread>b__0(Device d) in ***.cs:line 165

解决方法

Thread t = new Thread(work, 512);

2d2986b6454cae0b82013344001f0e8.png

api 参考:

Thread 构造函数 (System.Threading) | Microsoft Learn