more G-Labs products

Author Topic: High memory usage  (Read 1460 times)

May 19, 2015, 11:22:45 AM
Read 1460 times

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
The homegenie use a lot of memory, when my program ( http://www.homegenie.it/forum/index.php?topic=693.0 ) is running.
Memory usage:
2015.05.13. 17:44        40 MB
2015.05.14. 09:46      218 MB
2015.05.14. 19:00      396 MB
2015.05.14. 22:43      495 MB
2015.05.14. 23:21      505 MB
2015.05.15. 10:31      664 MB
2015.05.15. 11:45      682 MB
2015.05.15. 13:53      716 MB
2015.05.15. 19:05      785 MB
2015.05.15. 21:16      820 MB
2015.05.17. 20:40    1410 MB
2015.05.18. 08:55    1573 MB
2015.05.18. 17:00    1666 MB

I made a few test.... When this lines removed, the memory usage is not growing.
Code: [Select]
  .OnStringReceived( HandleStringReceived )
  .OnMessageReceived( HandleMessageReceived )
  .OnStatusChanged( HandleStatusChanged )

What could be the problem?
« Last Edit: May 19, 2015, 04:09:22 PM by KaZe »

June 05, 2015, 07:54:18 AM
Reply #1

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
Can you advise me how to find out which part of the program increase the memory usage?

June 05, 2015, 01:43:27 PM
Reply #2

kevin1

  • *****
  • Information
  • Hero Member
  • Posts: 330
I use SerialPort for my arduino and I don't have any fixes for you.  I can say that it seems like SerialPort can cause a big CPU load.  My arduino is sending a small status message once per second for all my sensors.  At one time my laptop fan was running non-stop, cranking out serious hot air.  If I remember correctly, stopping HG SerialPort seemed to stop the problem (fan slowed and cooler air).  FYI...This is an older i7 CPU Win7 laptop that shouldn't be overloaded by one 50 byte serial port message per second.

I don't seem to be having this problem now, not sure what is different.

Maybe change the title of your post to "SerialPort Memory Leak?" to see if that catches Gene's attention ;-)

Gene - are there any other commands that we should be calling to clean up after processing SerialPort messages (Dispose, Collect,?)?  I started with the SerialPort example, so if it wasn't in there, I'm not calling it.

Googling "csharp serial port memory leak" has some hits but hard for me to tell if these are relevant...

http://stackoverflow.com/questions/765285/serialport-memory-leak-in-net-2-0-3-5

http://stackoverflow.com/questions/4260380/memory-leak-using-net-serialport-is-there-a-better-way    -- This discusses new thread is created for every message(?)... has a workaround...
Quote
DataReceived is executed on a different thread. I had problems with really fast data and this event caused me problems. Because of that, I created one thread and read the data myself:
Code: [Select]
while (this.serialPort.IsOpen)
{
    int b = this.serialPort.ReadByte();
    if (b != -1)
    {
        // data is good here
    }
}


« Last Edit: June 05, 2015, 01:46:16 PM by kevin1 »

June 05, 2015, 04:51:57 PM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I don't know anything that can help with this, but I wanted to point out that sending a 50-byte packet over serial once per second will not cause an issue with performance on it's own.  However, it's possible that, depending on how the command is instantiated, it could keep the CPU running at a higher clock speed.  This is an issue with cell phones that eat batteries in that some application can keep the phone from going into sleep mode and the CPU runs at a higher than needed speed generating heat and using up the battery.  The solution might be finding an alternative way to obtain the data that doesn't keep the CPU at full speed (if that's truly the issue).

June 05, 2015, 05:32:34 PM
Reply #4

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
My CPU runs usually between 1-10%. I have nothing wrong with CPU. My problem is memory usage. 2 GB of ram eaten within a short period of time.