StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.89k stars 1.51k forks source link

OutOfMemory exception #2517

Closed chenyigg closed 1 year ago

chenyigg commented 1 year ago

I automatically inject this RedisService class through the controller of AbpVNext, and call some internal stringget, stringset and other methods, but I find that when accessing the program interface in large quantities (RedisService is used in the interface), the memory occupied by the program is always too high and will not decrease until it explodes the server. Is it because there is something wrong with the writing of this class, and if so, which part should be changed? I hope to get some help. Thank you.(To add, when I use the visual studio memory analysis tool, I find that pipelines.sockets.unofficial.arenas.arena < stackexchange.redis.rawr > takes up a lot of Memory.)

    public interface IRedisService : IScopedDependency
    {
        IDatabase CreatePipeline();
        ITransaction CreateTransaction();
        ITransaction CreateTransactionAsync();
        void Dispose();
        Some method......
    }

public class RedisService : IRedisService, IDisposable
    {
        internal readonly ConnectionMultiplexer _redis;

        private readonly Lazy<ConnectionMultiplexer> _redisLazy;
        private bool _disposed = false;

        public RedisService()
        {
            string connectionString = Config.GetStr(AppDomain.CurrentDomain.BaseDirectory + $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", "Redis:Configuration");
            _redisLazy = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(connectionString));
            _redis = _redisLazy.Value;
        }

        public IDatabase GetDatabase()
        {
            return _redis.GetDatabase();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _redisLazy.Value.Dispose();
                }

                _disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #region String
        /// <summary>
        /// string类型获取值,同步
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T StringGet<T>(string key)
        {
            var db = _redis.GetDatabase();
            var value = db.StringGet(key);
            if (!value.HasValue)
            {
                return default(T);
            }
            return JsonConvert.DeserializeObject<T>(value);
        }
}
chenyigg commented 1 year ago

Version:Volo.Abp.Caching.StackExchangeRedis 6.0

mgravell commented 1 year ago

What is the creation frequency here? The "scoped dependency" makes me worry this is per-scope; a multiplexer should basically be a singleton (or a pseudo-singleton)

On Wed, 2 Aug 2023, 09:04 chenyigg, @.***> wrote:

Version:Volo.Abp.Caching.StackExchangeRedis 6.0

— Reply to this email directly, view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/2517#issuecomment-1661707618 or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEHMHYISR26ZCCGU42MB3XTICZJBFKMF2HI4TJMJ2XIZLTSOBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDUOJ2WLJDOMFWWLLTXMF2GG2C7MFRXI2LWNF2HTAVFOZQWY5LFUVUXG43VMWSG4YLNMWVXI2DSMVQWIX3UPFYGLLDTOVRGUZLDORPXI6LQMWWES43TOVSUG33NNVSW45FGORXXA2LDOOJIFJDUPFYGLKTSMVYG643JORXXE6NFOZQWY5LFVAYTONZUHAYTQM4CUR2HS4DFUVUXG43VMWSXMYLMOVS2UMJYGMZDMNJSGYZTTJ3UOJUWOZ3FOKTGG4TFMF2GK . You are receiving this email because you are subscribed to this thread.

Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub .

chenyigg commented 1 year ago

What is the creation frequency here? The "scoped dependency" makes me worry this is per-scope; a multiplexer should basically be a singleton (or a pseudo-singleton) On Wed, 2 Aug 2023, 09:04 chenyigg, @.***> wrote: Version:Volo.Abp.Caching.StackExchangeRedis 6.0 — Reply to this email directly, view it on GitHub <#2517 (comment)> or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEHMHYISR26ZCCGU42MB3XTICZJBFKMF2HI4TJMJ2XIZLTSOBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDUOJ2WLJDOMFWWLLTXMF2GG2C7MFRXI2LWNF2HTAVFOZQWY5LFUVUXG43VMWSG4YLNMWVXI2DSMVQWIX3UPFYGLLDTOVRGUZLDORPXI6LQMWWES43TOVSUG33NNVSW45FGORXXA2LDOOJIFJDUPFYGLKTSMVYG643JORXXE6NFOZQWY5LFVAYTONZUHAYTQM4CUR2HS4DFUVUXG43VMWSXMYLMOVS2UMJYGMZDMNJSGYZTTJ3UOJUWOZ3FOKTGG4TFMF2GK . You are receiving this email because you are subscribed to this thread. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub .

In concurrent testing, there are about 200 requests lasting 5-10 minutes.

chenyigg commented 1 year ago

Can changing IScopedDependency to ISingletonDependency solve the problem?

What is the creation frequency here? The "scoped dependency" makes me worry this is per-scope; a multiplexer should basically be a singleton (or a pseudo-singleton) On Wed, 2 Aug 2023, 09:04 chenyigg, @.***> wrote: Version:Volo.Abp.Caching.StackExchangeRedis 6.0 — Reply to this email directly, view it on GitHub <#2517 (comment)> or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEHMHYISR26ZCCGU42MB3XTICZJBFKMF2HI4TJMJ2XIZLTSOBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDUOJ2WLJDOMFWWLLTXMF2GG2C7MFRXI2LWNF2HTAVFOZQWY5LFUVUXG43VMWSG4YLNMWVXI2DSMVQWIX3UPFYGLLDTOVRGUZLDORPXI6LQMWWES43TOVSUG33NNVSW45FGORXXA2LDOOJIFJDUPFYGLKTSMVYG643JORXXE6NFOZQWY5LFVAYTONZUHAYTQM4CUR2HS4DFUVUXG43VMWSXMYLMOVS2UMJYGMZDMNJSGYZTTJ3UOJUWOZ3FOKTGG4TFMF2GK . You are receiving this email because you are subscribed to this thread. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub .

NickCraver commented 1 year ago

@chenyigg Yep, you almost certainly want to be singleton here, not recreating multiplexers per request/scope.

chenyigg commented 1 year ago

It did work. Thank you