more G-Labs products

Author Topic: TcpClientHelper memory management problem  (Read 3026 times)

July 17, 2015, 09:44:15 PM
Read 3026 times

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
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();

October 27, 2015, 09:01:37 AM
Reply #1

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
This phenomenon occurs only for me?  :-[ Or nothing else use a TCP connection?  ???

October 27, 2015, 10:07:23 AM
Reply #2

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi KaZe,

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

g.

October 27, 2015, 10:30:51 AM
Reply #3

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
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.
« Last Edit: October 27, 2015, 11:52:52 AM by KaZe »

October 27, 2015, 05:27:36 PM
Reply #4

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.

October 27, 2015, 05:58:49 PM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.

October 27, 2015, 09:30:19 PM
Reply #6

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
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?
« Last Edit: October 27, 2015, 09:39:14 PM by KaZe »

October 27, 2015, 10:13:29 PM
Reply #7

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
I try with version 498 and new NetClientLib.dll. The problem is the same.

October 27, 2015, 11:09:36 PM
Reply #8

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
sorry KaZe, there's a bug in the library... I'll fix it and let you know.

g.

October 28, 2015, 01:14:54 AM
Reply #9

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.



October 28, 2015, 03:47:49 PM
Reply #10

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
KaZe did you have any change of verifying this with latest r499?


October 28, 2015, 03:51:44 PM
Reply #11

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
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

October 28, 2015, 03:53:16 PM
Reply #12

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.

October 28, 2015, 03:55:54 PM
Reply #13

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Can you also put a Program.Notify in the HandleMessageReceived ? There might be some sort of loop between the client and server?

October 28, 2015, 03:58:36 PM
Reply #14

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
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.