Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A memory leak occurs at CSRedisClient #488

Open
whiteneon1017 opened this issue Oct 17, 2023 · 1 comment
Open

A memory leak occurs at CSRedisClient #488

whiteneon1017 opened this issue Oct 17, 2023 · 1 comment

Comments

@whiteneon1017
Copy link

If you create an instance of CSRedisClient and release it repeatedly, about 36 bytes will be leaked each time.
To resolve the problem, it is necessary to write the Dispose method in the following three places.

leak test tool

{
    for (int index = 0; ; index++)
    {
        System.Threading.Thread.Sleep(1000);

        client = new CSRedisClient(url);

        client.Dispose();
        client = null;
    }
}

Additions

    public partial class CSRedisClient : IDisposable
    {
        public void Dispose()
        {
            foreach (var pool in this.Nodes.Values) pool.Dispose();
            SentinelManager?.Dispose();

            this.Nodes.Clear(); //add
            this.NodesKey.Clear(); //add
            this.NodesIndex.Clear(); //add
            this.SlotCache.Clear(); //add
            this._rnd = null; //add
            this.NodesServerManager = null; //add
            this.NodeRuleRaw = null; //add
            this.NodeRuleExternal = null; //add
            this.NodesLock = null; //add
            this.BackgroundGetSentinelMasterValueIngLock = null; //add
        }
    }

    public partial class RedisClient : IRedisClientSync, IRedisClientAsync
    {
        /// <summary>
        /// Dispose all resources used by the current RedisClient
        /// </summary>
        public void Dispose()
        {
            Socket.Dispose(); //add
            _subscription.MessageReceived -= OnSubscriptionReceived; //add
            _subscription.Changed -= OnSubscriptionChanged; //add
            _monitor.MonitorReceived -= OnMonitorReceived; //add
            _connector.Connected -= OnConnectionConnected; //add
            _transaction.TransactionQueued -= OnTransactionQueued; //add
            
            _connector.Dispose();
        }
    }
    public class RedisClientPool : ObjectPool<RedisClient>
    {
        public new void Dispose() //add
        {
            base.Dispose();

            if (_policy != null)
            {
                _policy._pool = null;
                _policy = null;
            }

            Encoding = null;
        }
    }

@2881099
Copy link
Owner

2881099 commented Oct 18, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants