HomeGenie Forum

Development => Bug reporting => Topic started by: KaZe on July 17, 2015, 09:44:15 PM

Title: TcpClientHelper memory management problem
Post by: KaZe on July 17, 2015, 09:44:15 PM
I did a test with this code. This is a simple connection to a TCP server with status feedback. Nothing else.
Still, steadily increasing memory usage when running the program.

Homegenie use of memory:
2015.07.17 12:46   56 MB
2015.07.17 20:40   225 MB

If the program not running, the memory usage 41-43 MB continuously.


Code: [Select]
var remoteServer = "10.1.1.131";
var remotePort = 6000;
var NetworkRelaysModules = Modules.InDomain("HomeAutomation.NetworkRelays");
 
Action<bool> HandleStatusChanged = (bool connected) =>
  {
   Program.Notify("TCP Client", "Connected = " + connected);
  };

Program.Notify("TCP Client", "Starting");

TcpClient
  .OnStatusChanged( HandleStatusChanged )
  .Service( remoteServer )
  .Connect( remotePort );

Program.Notify("TCP Client", "Started");
Program.Parameter("Status.Level").Value = "1";

Program.GoBackground();
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on October 27, 2015, 09:01:37 AM
This phenomenon occurs only for me?  :-[ Or nothing else use a TCP connection?  ???
Title: Re: TcpClientHelper memory management problem
Post by: Gene on October 27, 2015, 10:07:23 AM
Hi KaZe,

please give more details, like complete program code and version of HG you're using.

g.
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on October 27, 2015, 10:30:51 AM
Hi Gene!

I use HG version 1.1 beta r498. I attached my program, what I use.
The program in my first post is simple program, what is only connect to TCP relay and receive the connection messege from relay. Nothing else. But unfortunately this little program increasing memory usage too.
I think the TCP connection don't "emptying" the memory.
Title: Re: TcpClientHelper memory management problem
Post by: Gene on October 27, 2015, 05:27:36 PM
Hi KaZe,

try changing your main loop code like this:

Code: [Select]
TcpClient
  .OnMessageReceived( HandleMessageReceived )
  .OnStringReceived( HandleStringReceived )
  .OnStatusChanged( HandleStatusChanged )
  .Service( server );

while (Program.IsEnabled)
{
  string server = Program.Option("IPAddress").Value.Trim();
  string port = Program.Option("Port").Value.Trim();

  if (!TcpClient.IsConnected)
  {
    if (!String.IsNullOrWhiteSpace(server) &amp;&amp; !server.EndsWith("?") &amp;&amp; !String.IsNullOrWhiteSpace(port))
    {
      TcpClient.Connect( Convert.ToInt32(port) );
    }
    else
    {
      Program.Notify("Network Relays", "Kérlek állíts be egy valós IP címet és egy portot!");
    }
  }

  Pause(5);
}

let me know if this work as it can give me some hint about the solution.

g.
Title: Re: TcpClientHelper memory management problem
Post by: Gene on October 27, 2015, 05:58:49 PM
There was a missing call to Socket.Dispose on the Disconnect/Reconnect, though the Socket.Close method was correctly in place.
So if the previous experiment fails, try replacing the attacched library and see if it fixes the problem.

g.
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on October 27, 2015, 09:30:19 PM
Hi Gene!

I install version 499 HG. Now I can't start my program. "Object reference not set to an instance of an object"

What went wrong?
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on October 27, 2015, 10:13:29 PM
I try with version 498 and new NetClientLib.dll. The problem is the same.
Title: Re: TcpClientHelper memory management problem
Post by: Gene on October 27, 2015, 11:09:36 PM
sorry KaZe, there's a bug in the library... I'll fix it and let you know.

g.
Title: Re: TcpClientHelper memory management problem
Post by: Gene on October 28, 2015, 01:14:54 AM
Hi KaZe,

I ended up rewriting the TcpClient class used by the TcpClientHelper because it was wrote for net 4.0. But now there's an official TcpClient class from Net 4.5.
So I repackaged r499 with the modifications and hopefully it will work now.
Let me know.

Cheers,
g.


Title: Re: TcpClientHelper memory management problem
Post by: Gene on October 28, 2015, 03:47:49 PM
KaZe did you have any change of verifying this with latest r499?

Title: Re: TcpClientHelper memory management problem
Post by: KaZe on October 28, 2015, 03:51:44 PM
Hi Gene!

Right now I wanted to write. Unfortunately It does not resolve the problem. :(
Install v499 at morning, modify the program code as you suggested. This is memory usage:

2015.10.28   09:10   123MB
2015.10.28   09:35   140MB
2015.10.28   10:55   153MB
2015.10.28   12:15   164MB
2015.10.28   15:48   211MB
Title: Re: TcpClientHelper memory management problem
Post by: Gene on October 28, 2015, 03:53:16 PM
Can it be something else you are using that is causing the problem?
How can I replicate the issue?
Are you having the UPnP interface enabled?

g.
Title: Re: TcpClientHelper memory management problem
Post by: Gene on October 28, 2015, 03:55:54 PM
Can you also put a Program.Notify in the HandleMessageReceived ? There might be some sort of loop between the client and server?
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on October 28, 2015, 03:58:36 PM
I turn off the program now. And I see the memory usage at 20:00. Usually the memory grow 15MB/hour. I write information to this topic at 20:10.
BTW UPNP service activated, but I use it rarely.
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on October 28, 2015, 08:27:38 PM
Hi Gene!

The memory changes without the program:
2015.10.28   15:48   211MB
2015.10.28   18:05   216MB
2015.10.28   20:16   223MB

It's a normal changes. I put the program.Notify in the HandleMessageReceived.
But, if you think, I can "share" the relay device on the internet, so you can connect to it...
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on October 28, 2015, 09:00:27 PM
HandleMessageReceived section work:
1.I turn on the one switch, the WebServiceCallReceived section send a control on command.
2. Network relay modul send a response message.
3. HandleMessageReceived handle this:
3.1 If the message not STATUS message (like control on response message), HandleMessageReceived send a STATUS reguest to a relay modul
3.2 If the message STATUS message, the HandleMessageReceived check the virtual switches status and the relays status. If does not match, toggle the virtual switch.

I attach the screenshot from the switch1 turn on and off log. And then HandleMessageReceived not send and not reveive message, just if I change the switch status (on/off)
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on November 01, 2015, 09:04:58 PM
Hi Gene!

I think the TCP connection works fine. Big thanks for it!!!  ;) I started a 'big' test. Turn off all program except Network relays TCP and Z-wave devices. It's take a long time, but I share my resolutions now, and I continue my testing, until I can't realise the problem.

Quote
2015.10.31   21:42   117MB only run Network relays TCP program + z-wave devices
2015.10.31   21:45   112MB
2015.10.31   23:53   113MB
2015.11.01   08:18   126MB
2015.11.01   08:18   121MB open web interface
2015.11.01   08:20   122MB start virtual switches program
2015.11.01   11:26   118MB
2015.11.01   11:28   120MB start security alarm system program
2015.11.01   11:33   123MB
2015.11.01   11:53   126MB
2015.11.01   12:50   118MB
2015.11.01   20:49   129MB
2015.11.01   20:50   132MB open web interface
2015.11.01   21:02   134MB start E-mail Account and Pushbullet program
Title: Re: TcpClientHelper memory management problem
Post by: KaZe on November 04, 2015, 01:31:02 PM
Hi Gene!

I finished the test. The TCP/IP connection works fine. Memory management fine.
And I found the program, which "eat" my memory. Naturally, the last program in the list... :D
Hear is my test result:
Quote
2015.10.31   21:42   117MB only run Network relays program + z-wave
2015.10.31   21:45   112MB
2015.10.31   23:53   113MB
2015.11.01   08:18   126MB
2015.11.01   08:18   121MB open web interface
2015.11.01   08:20   122MB start virtual switches program
2015.11.01   11:26   118MB
2015.11.01   11:28   120MB start security alarm system program
2015.11.01   11:33   123MB
2015.11.01   11:53   126MB
2015.11.01   12:50   118MB
2015.11.01   20:49   129MB
2015.11.01   20:50   132MB open web interface
2015.11.01   21:02   134MB start E-mail Account and Pushbullet program
2015.11.01   21:49   142MB
2015.10.01   22:04   131MB
2015.10.02   00:40   140MB
2015.10.02   00:48   133MB
2015.10.02   07:11   131MB
2015.10.02   09:16   131MB
2015.10.02   09:17   135MB start Lights/Level Memory and Smart Lights
2015.11.02   13:36   135MB
2015.11.02   13:41   133MB start Energy Management/Energy monitor
2015.11.02   17:37   131MB
2015.11.02   17:39   132MB start Weather and Enviroment/Weather Underground
2015.11.02   17:43   136MB
2015.11.02   18:30   132MB
2015.11.02   18:53   133MB
2015.11.02   19:04   139MB
2015.11.02   21:26   149MB
2015.11.02   21:46   145MB
2015.11.02   22:02   150MB start Device and Things/Generic IP Camera
2015.11.02   22:11   146MB
2015.11.02   22:26   149MB
2015.11.03   07:43   151MB
2015.11.03   07:44   149MB start Z-Wave/level poll
2015.11.03   09:57   153MB
2015.11.03   12:20   151MB
2015.11.03   12:24   151MB start Security/Ping Me at Home Extended
2015.11.03   15:05   151MB
2015.11.03   17:35   156MB
2015.11.03   18:02   151MB
2015.11.03   18:09   155MB start Security/Auto Arm Disarm
2015.11.03   18:19   159MB
2015.11.03   18:20   152MB
2015.11.03   20:36   157MB
2015.11.03   20:55   160MB start Security/Presence Simulation
2015.11.03   21:17   155MB
2015.11.04   09:23   252MB
2015.11.04   08:32   254MB
2015.11.04   13:14   249MB

I attach the program, which eat a memory. When the program run, it's wait a startup event, like virtual switch on. So, if the virtual switch is off, the program not running, just waiting (yellow status)
What is the problem with this program?