{"action":"created","issue":{"url":"https://api.github.com/repos/nats-io/csnats/issues/93","repository_url":"https://api.github.com/repos/nats-io/csnats","labels_url":"https://api.github.com/repos/nats-io/csnats/issues/93/labels{/name}","comments_url":"https://api.github.com/repos/nats-io/csnats/issues/93/comments","events_url":"https://api.github.com/repos/nats-io/csnats/issues/93/events","html_url":"https://github.com/nats-io/csnats/issues/93","id":169285242,"number":93,"title":"Potential memory leak when closing connection quickly after opening it","user":{"login":"RobertZFord","id":7231568,"avatar_url":"https://avatars.githubusercontent.com/u/7231568?v=3","gravatar_id":"","url":"https://api.github.com/users/RobertZFord","html_url":"https://github.com/RobertZFord","followers_url":"https://api.github.com/users/RobertZFord/followers","following_url":"https://api.github.com/users/RobertZFord/following{/other_user}","gists_url":"https://api.github.com/users/RobertZFord/gists{/gist_id}","starred_url":"https://api.github.com/users/RobertZFord/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RobertZFord/subscriptions","organizations_url":"https://api.github.com/users/RobertZFord/orgs","repos_url":"https://api.github.com/users/RobertZFord/repos","events_url":"https://api.github.com/users/RobertZFord/events{/privacy}","received_events_url":"https://api.github.com/users/RobertZFord/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-08-04T03:48:37Z","updated_at":"2016-08-04T15:04:29Z","closed_at":null,"body":"I noticed in a loop that opens a new connection, then rapidly closes it (say there was a very small amount of data to transmit intermittently, but a persisted connection was not desired), that resources are not released as expected, and if the loop runs for long enough, an OutOfMemoryException is thrown.  I found that a brief pause before closing the connection is sufficient to avoid the problem.\r\n\r\nI have tested this in .NET 4.5, and .NET 4.6.1, using the NATS.Client 0.5.0 NuGet package, and in Debug and Release configuration.\r\n\r\nTo reproduce:\r\nStep 1:\r\nCreate a .NET 4.6.1 C# Console application in VS.  I used VS2015 Community Edition)\r\n\r\nStep 2:\r\nInstall the NATS.Client package from NuGet.  I ran `install-package NATS.Client` from the Package Manager Console.\r\n\r\nStep 3:\r\nPut the following code (C# 6.0) in the Program.cs file:\r\n```\r\n#define MEMORYLEAKSAREAWESOME\r\n\r\nusing static System.Console;\r\nusing static System.Threading.Thread;\r\nusing static System.Diagnostics.Stopwatch;\r\nusing static System.Diagnostics.Process;\r\nusing static System.Globalization.CultureInfo;\r\n\r\nusing NATS.Client;\r\n\r\nnamespace NatsLeakExample\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            var proc = GetCurrentProcess();\r\n            var sw = StartNew();\r\n            bool continueTest = true;\r\n            ConnectionFactory cf = new ConnectionFactory();\r\n            while (continueTest)\r\n            {\r\n                using (IConnection conn = cf.CreateConnection())\r\n                {\r\n#if !MEMORYLEAKSAREAWESOME\r\n                    Sleep(100); // brief pause in the connection lifetime dodges the problem\r\n#endif\r\n                }\r\n#if MEMORYLEAKSAREAWESOME\r\n                Sleep(100); // this will not affect the connection, and a memory leak will result\r\n#endif\r\n                if (sw.ElapsedMilliseconds > 300000)\r\n                {\r\n                    continueTest = false;\r\n                }\r\n            }\r\n            sw.Stop();\r\n            long privateBytesUsed = proc.PrivateMemorySize64;   // I chose PrivateMemory as it reflected the memory usage graph in VS debugging\r\n            string formattedBytesUsed = privateBytesUsed.ToString(\"#,##0,,M\", InvariantCulture);\r\n\r\n#if MEMORYLEAKSAREAWESOME\r\n            string testStatus = \"set\";\r\n#else\r\n            string testStatus = \"not set\";\r\n#endif\r\n\r\n            WriteLine($\"Memory used after 5 minutes when {testStatus} to enable the leak : {formattedBytesUsed}\");\r\n        }\r\n    }\r\n}\r\n```\r\nWhen `MEMORYLEAKSAREAWESOME` is defined, the memory leak will occur.  When it is not defined, the memory leak will not occur.\r\n\r\nStep 4:\r\nRun the program.\r\n\r\nRelease output when `MEMORYLEAKSAREAWESOME` is defined:\r\n```\r\nMemory used after 5 minutes when set to enable the leak : 347M\r\nPress any key to continue . . .\r\n```\r\n\r\nRelease output when `MEMORYLEAKSAREAWESOME` is not defined:\r\n```\r\nMemory used after 5 minutes when not set to enable the leak : 16M\r\nPress any key to continue . . .\r\n```\r\n\r\n\r\nThis is just what I've noted, and created a proof of concept for.  It may also be related to [Issue #91](https://github.com/nats-io/csnats/issues/91)."},"comment":{"url":"https://api.github.com/repos/nats-io/csnats/issues/comments/237581481","html_url":"https://github.com/nats-io/csnats/issues/93#issuecomment-237581481","issue_url":"https://api.github.com/repos/nats-io/csnats/issues/93","id":237581481,"user":{"login":"ColinSullivan1","id":11180189,"avatar_url":"https://avatars.githubusercontent.com/u/11180189?v=3","gravatar_id":"","url":"https://api.github.com/users/ColinSullivan1","html_url":"https://github.com/ColinSullivan1","followers_url":"https://api.github.com/users/ColinSullivan1/followers","following_url":"https://api.github.com/users/ColinSullivan1/following{/other_user}","gists_url":"https://api.github.com/users/ColinSullivan1/gists{/gist_id}","starred_url":"https://api.github.com/users/ColinSullivan1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ColinSullivan1/subscriptions","organizations_url":"https://api.github.com/users/ColinSullivan1/orgs","repos_url":"https://api.github.com/users/ColinSullivan1/repos","events_url":"https://api.github.com/users/ColinSullivan1/events{/privacy}","received_events_url":"https://api.github.com/users/ColinSullivan1/received_events","type":"User","site_admin":false},"created_at":"2016-08-04T15:04:29Z","updated_at":"2016-08-04T15:04:29Z","body":"@RobertZFord Thank you for raising the issue - I'll check this out."}}
{"actor":{"display_login":"ColinSullivan1"}}