首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在IIS中,System.Random.NextBytes会占用大量cpu资源

问在IIS中,System.Random.NextBytes会占用大量cpu资源
EN

Stack Overflow用户
提问于 2019-03-21 13:19:54
回答 1查看 42关注 0票数 3

今天,我的IIS站点占用了很高的cpu,我的服务器非常慢!我从任务管理器那里得到一个转储文件,然后执行IISReset,问题就解决了。

在windbg中,我使用!runaway来查找执行了很长时间的线程,比如:enter image description here

并使用!clrstack找出前10个线程正在执行以下代码: System.Random.NextBytes(Byte[])),如下所示:

代码语言:javascript
复制
Thread  30
Current frame: (MethodDesc 00007ffb5252c418 +0x20 System.Random.InternalSample())
Child-SP         RetAddr          Caller, Callee
000000487470b460 00007ffb531d8138 (MethodDesc 00007ffb5252c458 +0x28 System.Random.NextBytes(Byte[]))
000000487470b4b0 00007ffaffafe87a (MethodDesc 00007ffafeeb4e20 +0x4a Pinpoint.Core.Util.SpanIdUtil.GetNewSpanId())
000000487470b4e0 00007ffb014a53fb (MethodDesc 00007ffafeeb4e30 +0x1b Pinpoint.Core.Util.SpanIdUtil.GetNextSpanId(Int64, Int64))
000000487470b520 00007ffb014a5370 (MethodDesc 00007ffafeeb4150 +0x30 Pinpoint.Core.TraceId.GetNextTraceId())
000000487470b5a0 00007ffaffae4dab (MethodDesc 00007ffaff317ed0 +0x1cb Mike.Pinpoint.Plugin.WebRequestPlugins.TraceHttpWebRequest.BeforeGetResponse(System.Net.HttpWebRequest))
000000487470bbb0 00007ffb529444e0 (MethodDesc 00007ffb52535888 +0x80 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[]))
000000487470bcf0 00007ffb529444e0 (MethodDesc 00007ffb52535888 +0x80 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[]))
000000487470bd60 00007ffb5292f4ee (MethodDesc 00007ffb52535870 +0x8e System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo))
000000487470bde0 00007ffaffae4b79 (MethodDesc 00007ffaff318238 +0x99 Mike.Pinpoint.Plugin.WebRequestPlugins.TraceFilterImpl.BeforeGetResponseForFilter(System.Net.HttpWebRequest))
000000487470bdf0 00007ffb528f93e0 (MethodDesc 00007ffb526b1270 +0x120 System.IO.StreamWriter.Dispose(Boolean))

下面是我的原始代码:

代码语言:javascript
复制
using System;

namespace Pinpoint.Core.Util
{
    public class SpanIdUtil
    {
        private static readonly Random rnd = new Random(Environment.TickCount);
        public const long Null = -1;

        public static long GetNewSpanId()
        {
            byte[] buf = new byte[8];
            rnd.NextBytes(buf);
            long newId = BitConverter.ToInt64(buf, 0);
            return newId;
        }

        public static long GetNextSpanId(long spanId, long parentSpanId)
        {
            long newId = GetNewSpanId();

            while (newId == spanId || newId == parentSpanId)
            {
                newId = GetNewSpanId();
            }
            return newId;
        }
    }
}

谁能告诉我,是什么导致了这个错误?注意,这种情况只发生过一次。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-21 13:32:17

很可能是因为您在多个线程之间共享相同的Random实例,所以在随机类中遇到了一个问题,如here所述,在这个类中,返回零可能会卡住。随机不是线程安全的。

就我个人而言,在服务器应用程序上,我永远不会在没有倒计时保护的情况下使用while循环,如果它到了零,就会抛出异常。除非你能(从数学上)证明你的循环会结束,否则你应该防止它卡住,因为在服务器应用程序中,当它卡住时,你已经永远失去了一个线程。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55274210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档