{"action":"created","issue":{"url":"https://api.github.com/repos/chkr1011/MQTTnet/issues/230","repository_url":"https://api.github.com/repos/chkr1011/MQTTnet","labels_url":"https://api.github.com/repos/chkr1011/MQTTnet/issues/230/labels{/name}","comments_url":"https://api.github.com/repos/chkr1011/MQTTnet/issues/230/comments","events_url":"https://api.github.com/repos/chkr1011/MQTTnet/issues/230/events","html_url":"https://github.com/chkr1011/MQTTnet/issues/230","id":316496212,"number":230,"title":"Possible Memory Leak","user":{"login":"VladimirAkopyan","id":12278048,"avatar_url":"https://avatars0.githubusercontent.com/u/12278048?v=4","gravatar_id":"","url":"https://api.github.com/users/VladimirAkopyan","html_url":"https://github.com/VladimirAkopyan","followers_url":"https://api.github.com/users/VladimirAkopyan/followers","following_url":"https://api.github.com/users/VladimirAkopyan/following{/other_user}","gists_url":"https://api.github.com/users/VladimirAkopyan/gists{/gist_id}","starred_url":"https://api.github.com/users/VladimirAkopyan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/VladimirAkopyan/subscriptions","organizations_url":"https://api.github.com/users/VladimirAkopyan/orgs","repos_url":"https://api.github.com/users/VladimirAkopyan/repos","events_url":"https://api.github.com/users/VladimirAkopyan/events{/privacy}","received_events_url":"https://api.github.com/users/VladimirAkopyan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2018-04-21T14:25:44Z","updated_at":"2018-05-02T15:14:51Z","closed_at":null,"author_association":"NONE","body":"I am running MQTT server in a docker container, and i get strange behaviour where the amount of memory bloats into gigabytes. \r\n\r\n![image](https://user-images.githubusercontent.com/12278048/39085106-107f3bda-4576-11e8-9c0f-2461ebc116da.png)\r\n![image](https://user-images.githubusercontent.com/12278048/39085113-17d1e25c-4576-11e8-8720-4296dcf11e97.png)\r\n\r\nThe system is\r\nProducer ->  Server -> MultipleConsumers\r\nThe producer also uses MQTTnet, and sends ~100 messages per second, QOS 0\r\nThe consumers use MQTTnet too. they often disconnect without closing connection properly. \r\nThe behaviour only occurs if the clients connect, and then disconnect. If the server is started, the producer is connected and producing messages but no client connect, then memory use stays very low (<100MB) \r\nIt is not obvious to me why this is happening at this point. I could not reproduce it previously on my local machine in VS debugger, but I will try again soon. \r\n\r\nKubernetes Version - 1.7\r\n.Net SDK / Runtime : \r\n- 2.0 \r\n- 2.1 preview 2\r\n\r\nThere is a related issue  - https://github.com/dotnet/coreclr/issues/14991#issuecomment-348428003\r\nHowever I made sure that this isn't the issue appearing here by running `GC.Collect()` every second - the issue still persists. \r\n\r\n**Server Code**\r\n```C#\r\npublic static readonly DateTime StartTimeUTC = DateTime.UtcNow;\r\n\r\nstatic void Main(string[] args)\r\n{\r\n   Console.WriteLine(\"Server Started!\");\r\n   Task.Run(async () => await StartServer()).Wait();\r\n}\r\n\r\nprivate static async Task StartServer()\r\n{\r\n\tvar optionsBuilder = new MqttServerOptionsBuilder()\r\n\t\t.WithConnectionBacklog(100)\r\n\t\t.WithDefaultEndpointPort(1883);\r\n\tvar mqttServer = new MqttFactory().CreateMqttServer();\r\n\tawait mqttServer.StartAsync(optionsBuilder.Build());\r\n\tvar ct = new CancellationTokenSource();\r\n\tvar heartbeatTask = Task.Run(async () => await ServerHeartbeat(ct.Token, mqttServer)); \r\n  \r\n\tConsole.WriteLine(\"Type 'quit' to exit\");\r\n\r\n\tstring input = string.Empty; \r\n\twhile (true)\r\n\t{\r\n\t\t//Docker has a habit of sending random shit into CMD\r\n\t\tchar c = (char)Console.Read();\r\n\t\tinput +=  c; \r\n\r\n\t   if (input.Contains(\"quit\")) \r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Shutting Down...\");\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\telse if (input.Length > 10)\r\n\t\t{\r\n\t\t\tinput = input.Substring(input.Length - 3); \r\n\t\t}\r\n\t}\r\n\tct.Cancel();\r\n\tawait heartbeatTask;\r\n\tawait mqttServer.StopAsync();\r\n}\r\n\r\nprivate static async Task ServerHeartbeat(CancellationToken token, IMqttServer server)\r\n{\r\n  long heartbeat =0;\r\n  while (token.IsCancellationRequested == false)\r\n  {\r\n\tvar message = new MqttApplicationMessageBuilder()\r\n\t .WithTopic(\"$SYS/broker/uptime\")\r\n\t .WithPayload($\"{DateTime.UtcNow - StartTimeUTC}\")\r\n\t .WithAtMostOnceQoS()\r\n\t .WithRetainFlag(false)\r\n\t .Build();\r\n\tawait server.PublishAsync(message);\r\n\r\n\tmessage = new MqttApplicationMessageBuilder()\r\n\t .WithTopic(\"$SYS/broker/time\")\r\n\t .WithPayload($\"{DateTime.UtcNow}\")\r\n\t .WithAtMostOnceQoS()\r\n\t .WithRetainFlag(false)\r\n\t .Build();\r\n\tawait server.PublishAsync(message);\r\n\tawait Task.Delay(1000);\r\n}\r\n```\r\nAm I doing something dumb? I will update the ticket as I continue testing. "},"comment":{"url":"https://api.github.com/repos/chkr1011/MQTTnet/issues/comments/386013714","html_url":"https://github.com/chkr1011/MQTTnet/issues/230#issuecomment-386013714","issue_url":"https://api.github.com/repos/chkr1011/MQTTnet/issues/230","id":386013714,"user":{"login":"VLorz","id":5894765,"avatar_url":"https://avatars3.githubusercontent.com/u/5894765?v=4","gravatar_id":"","url":"https://api.github.com/users/VLorz","html_url":"https://github.com/VLorz","followers_url":"https://api.github.com/users/VLorz/followers","following_url":"https://api.github.com/users/VLorz/following{/other_user}","gists_url":"https://api.github.com/users/VLorz/gists{/gist_id}","starred_url":"https://api.github.com/users/VLorz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/VLorz/subscriptions","organizations_url":"https://api.github.com/users/VLorz/orgs","repos_url":"https://api.github.com/users/VLorz/repos","events_url":"https://api.github.com/users/VLorz/events{/privacy}","received_events_url":"https://api.github.com/users/VLorz/received_events","type":"User","site_admin":false},"created_at":"2018-05-02T15:14:51Z","updated_at":"2018-05-02T15:14:51Z","author_association":"NONE","body":"Hi Christian, same issue with version 2.7.5-rc1, yet it took longer than with previous version.\r\nRegards,\r\nVictor"}}
{"actor":{"display_login":"VLorz"}}