c# - ThreadPool 在短短几秒钟内使用了过多的内存

标签 c# threadpool

我制作了一个简单的控制台应用程序来打印质数。我将 ThreadPool 用于检查数字是否为质数的函数。

在任务管理器中,此程序开始占用过多内存(几秒钟内 1 GB) 如果我仍然必须使用 ThreadPool,该如何改进?

这是我写的代码

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(2);
        Console.WriteLine(3);
        Console.WriteLine(5);
        Console.WriteLine(7);
        Console.WriteLine(11);
        Console.WriteLine(13);
        Console.WriteLine(17);
        for (long i = 19; i < Int64.MaxValue; i = i+2)
        {
            if(i % 3 == 0 || i % 5 == 0 || i % 7 == 0 || i % 11 == 0 || i % 13 == 0 || i % 17 == 0 )
                continue;

            ThreadPool.QueueUserWorkItem(CheckForPrime, i);
        }
        Console.Read();
    }

    private static void CheckForPrime(object i)
    {
        var i1 = i as long?;
        var val =  Math.Sqrt(i1.Value);
        for (long j = 19; j <= val; j = j + 2)
        {
            if (i1 % j == 0) return;
        }
        Console.WriteLine(i1);

    }
}

最佳答案

修复代码的最简单方法,只需使用信号量限制工作队列;

class Program
{
    // Max 100 items in queue
    private static readonly Semaphore WorkLimiter = new Semaphore(100, 100);

    static void Main(string[] args)
    {
        Console.WriteLine(2);
        Console.WriteLine(3);
        Console.WriteLine(5);
        Console.WriteLine(7);
        Console.WriteLine(11);
        Console.WriteLine(13);
        Console.WriteLine(17);

        for (long i = 19; i < Int64.MaxValue; i = i + 2)
        {
            if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0 || i % 11 == 0 || i % 13 == 0 || i % 17 == 0)
                continue;

            // Get one of the 100 "allowances" to add to the queue.
            WorkLimiter.WaitOne();
            ThreadPool.QueueUserWorkItem(CheckForPrime, i);
        }
        Console.Read();
    }

    private static void CheckForPrime(object i)
    {
        var i1 = i as long?;
        try
        {
            var val = Math.Sqrt(i1.Value);
            for (long j = 19; j <= val; j = j + 2)
            {
                if (i1%j == 0) return;
            }
            Console.WriteLine(i1);
        }
        finally
        {
            // Allow another add to the queue
            WorkLimiter.Release();
        }
    }
}

这将允许您始终保持队列满(队列中有 100 个项目),而不会过度填充或添加 sleep

关于c# - ThreadPool 在短短几秒钟内使用了过多的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14767423/

相关文章:

c# - 获取 TFS 组成员

java - 使用正确的ExecutorService异步执行任务

c# - 创建用于测试的 DirectoryEntry 实例

c# - 由于单引号,XpathNavigator 给出 System.Xml.XPath.XPathException

ASP.NET HttpContext.Current 在 Task.Run 中

c# - 为什么通过调用 Task.Run 和 ThreadPool.QueueUserWorkItem 排队到 ThreadPool 时,线程数会增加一个以上?

使用 ThreadPool 的 C# Execute 方法(带参数)

java - 在所有线程(池中)完成后如何打印摘要消息?

c# - 为什么我的 ASP.NET Web 服务不能启动进程,但我的 .NET 控制台应用程序可以?

c# - AspNet.Identity CreateAsync 不返回 user.Id 与 CustomStore