2

dotnet C# 应用程序进程创建太多线程将会抛出 OutOfMemoryException 异常

 2 years ago
source link: https://lindexi.gitee.io/post/dotnet-C-%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E8%BF%9B%E7%A8%8B%E5%88%9B%E5%BB%BA%E5%A4%AA%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%B0%86%E4%BC%9A%E6%8A%9B%E5%87%BA-OutOfMemoryException-%E5%BC%82%E5%B8%B8.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
dotnet C# 应用程序进程创建太多线程将会抛出 OutOfMemoryException 异常

本文记录一个 dotnet 的特性,在应用程序快速创建大量线程的时候,将会因为线程创建时没有足够的资源而创建失败,此时将会抛出 OutOfMemoryException 异常,但实际进程占用内存不多

如使用以下逗比代码进行线程的创建

            while (true)
            {
                var thread = new Thread((_) =>
                {
                    Thread.Sleep(-1);
                });
                thread.Start();
            }

此时在运行时将会抛出 OutOfMemoryException 异常,在 x86 应用下,在我的设备上跑了大概 1000 个线程左右时将会炸掉,但是进程只占用了 60MB 左右的内存

c# - Why Thread.Start can throw OutOfMemoryException - Stack Overflow


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-C-%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E8%BF%9B%E7%A8%8B%E5%88%9B%E5%BB%BA%E5%A4%AA%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%B0%86%E4%BC%9A%E6%8A%9B%E5%87%BA-OutOfMemoryException-%E5%BC%82%E5%B8%B8.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号
lindexi%2F201985113622445

欢迎进入 Eleven 老师组建的 .NET 社区
lindexi%2F20209121930471745.jpg

以上广告全是友情推广,无盈利


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK