$_GET 内存不足?

发布于 2024-12-18 11:13:05 字数 931 浏览 0 评论 0原文

我正在创建一个多线程 random.org 数字获取器来实现到我的 c# IRC 机器人中。我遇到的问题是,它使用了相当大的内存占用。我认为它是 WebClient 类。我不喜欢它仅使用 ~5,000K 内存来连接到 URL、读取第一行并输出数字。

有没有更轻松的方法?

class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                Thread More = new Thread(GetRandomNum);
                More.Start();
            }
        }
        public static void GetRandomNum()
        {
            string number;
            for (int i = 0; i < 100; i++)
            {
                using (WebClient client = new WebClient())
                {
                    number = client.DownloadString("http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new");
                }
                Console.WriteLine(number.Trim());
            }
        }
    }

I'm creating a multi-threaded random.org number getter to implement into my c# IRC bot. The issue I have is, it uses a moderately big memory footprint. I think it's the WebClient class. I dislike how it uses ~5,000K memory just for connecting to the url, and reading the first line and outputting the number.

Is there a lighter approach to this?

class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                Thread More = new Thread(GetRandomNum);
                More.Start();
            }
        }
        public static void GetRandomNum()
        {
            string number;
            for (int i = 0; i < 100; i++)
            {
                using (WebClient client = new WebClient())
                {
                    number = client.DownloadString("http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new");
                }
                Console.WriteLine(number.Trim());
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-12-25 11:13:05

WebRequest 就可以了。

WebRequest request = WebRequest.Create ("http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new");
// If required by the server, set the credentials.

// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
// Display the status.

// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();

这看起来很复杂,但代码还是相当简单明了。
内存消耗必须比 WebClient 低得多。

WebRequest will do the trick.

WebRequest request = WebRequest.Create ("http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new");
// If required by the server, set the credentials.

// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
// Display the status.

// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();

This looks very complicated, still the code is rather simple and straightforward.
Memory consumption must be much lower than with WebClient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文