turbokongen / hass-AMS

Custom component reading AMS through MBus adapter into HomeAssistant
44 stars 10 forks source link

Memory leak #1

Closed Hellowlol closed 4 years ago

Hellowlol commented 4 years ago

The memory usage seems to increase over time. I suspect a memory leak

Ill try to fix this, but debugging this was/is rather painfull as the nuc running hassio is located in a really bad place for debugging as it take a really long time. My plan is to take a snapshot with tracemalloc every minute on nuc and just grab everything so i can sit another place and debug it :)

turbokongen commented 4 years ago

I have not notice this. I have a logging of the memory used on my server running HA. Those did not change, but hey, other systems might do other things. Happy to include your findings.

turbokongen commented 4 years ago

@Hellowlol Could you please test https://github.com/turbokongen/hass-AMS/tree/memory_leak to see if that takes care of the memory leak?

turbokongen commented 4 years ago

So, I've been debugging this, and here are my findings: Tried casting data variable in read_bytes() Tried emptying variables after use Tried flushing serial buffer Tried using .inWaiting() to read buffer, but this leads to high CPU usage again.

Whatever of the measures I've applied, seperately and alltogether don't work.

I've found where it comes from:

Filename: /home/pi/.homeassistant/custom_components/ams/__init__.py

Line #    Mem usage    Increment   Line Contents
================================================
    88  62.832031 MiB  62.832031 MiB       @profile(precision=6)
    89                                 def read_bytes(self):
    90                                     """Read the raw data from serial port."""
    91  62.832031 MiB   0.000000 MiB           byte_counter = 0
    92  62.832031 MiB   0.000000 MiB           bytelist = []
    93  62.832031 MiB   0.000000 MiB           buffer = bytes()
    94  62.843750 MiB   0.000000 MiB           while self._running:
    95  62.843750 MiB   0.011719 MiB               buffer = self._ser.read()
    96  62.843750 MiB   0.000000 MiB               if buffer:
    97  62.812500 MiB   0.000000 MiB                   bytelist.extend(buffer)
    98  62.812500 MiB   0.000000 MiB                   if buffer == FRAME_FLAG and byte_counter > 1:
    99  62.812500 MiB   0.000000 MiB                       self._ser.flushInput()
   100  62.812500 MiB   0.000000 MiB                       buffer = bytes()
   101  62.812500 MiB   0.000000 MiB                       _LOGGER.debug('buffer: %s', self._ser.inWaiting())
   102  62.812500 MiB   0.000000 MiB                       return bytelist
   103  62.812500 MiB   0.000000 MiB                   buffer = bytes()
   104  62.812500 MiB   0.000000 MiB                   byte_counter = byte_counter + 1
   105                                         else:
   106  62.843750 MiB   0.000000 MiB                   buffer = bytes()
   107  62.843750 MiB   0.000000 MiB                   continue

By the look of it, the issue seems to come from the pyserial lib.

Hellowlol commented 4 years ago

I have tried to debug the same, previously. I never figure out why the memory usage increased.