线程暴长~Quartz中创建Redis频繁后导致线程暴长

在最近项目开发过程中,在进行任务调度处理过程中,出现了一个问题,它的线程数暴长,从20多个可以到1000多个,如果你的服务器性能好的话,可以到10000多个,太恐怖了,就算你的服务再好,早晚有一天也会被new Redis炸干!哈哈!

原因:非托管资源使用过多,没有得到释放

一 连接网络资源过多,如redis,mongo,sql等

二 原生的数据库对象创建过多(自己没有连接池,现在的linq没这问题)

三 文件资源使用过多,没有得到释放

解决方法:

使用单例模式减少new操作的次数

对于我们应用程序的线程,如果它持续增长,那么,你就要看一下那么非托管资源是否被释放了,这个要重视起来。

有人说这个文章是个标题党,不知道从哪里发明的这个“新名词”,所以我把代码也发一下吧

/// <summary>
    /// Redis客户端
    /// </summary>
    public class RedisClient : IDisposable
    {
        public static RedisClient Instance;
        private ConnectionMultiplexer conn;
        private IDatabase cache;
        Private static Object lockObj=new Object();
        #region Constructors
        static RedisClient()
        {
            lock(lockObj)
            {
              Instance = new RedisClient();
            }
        }
        private RedisClient()
        {
            conn = ConnectionMultiplexer.Connect("localhost"); //var conn = ConnectionMultiplexer.Connect("redis0:6380,redis1:6380,allowAdmin=true");
            cache = conn.GetDatabase();
        }
        #endregion


        #region Behaviors
        public void Push(string key, object value)
        {
            cache.Push(key, value);
        }

        public object Pop(string key)
        {
            return cache.Pop(key);
        }

        public T Pop<T>(string key)
        {
            return cache.Pop<T>(key);
        }

        public T Get<T>(string key)
        {
            return cache.Get<T>(key);
        }

        public object Get(string key)
        {
            return cache.Get(key);
        }

        public void Set(string key, object value)
        {
            cache.Set(key, value);
        }
        #endregion

        #region IDisposable
        public void Dispose()
        {
            conn.Dispose();
        }
        #endregion


    }
原文地址:https://www.cnblogs.com/lori/p/4761193.html