title: Active BLE Scanning (BlueZ) - Issue with DBus
body: <p>I've started a project where I need to actively (all the time) scan for BLE Devices. I'm on Linux, using Bluez 5.49 and I use Python to communicate with dbus 1.10.20).
  I' m able to start scanning, stop scanning with bluetoothctl and get the BLE Advertisement data through DBus (GetManagedObjects() of the BlueZ interface). The problem I have is when I let the scanning for many hours, dbus-deamon start to take more and more of the RAM and I'm not able to find how to &quot;flush&quot; what dbus has gathered from BlueZ. Eventually the RAM become full and Linux isn't happy.</p>
  <p>So I've tried not to scan for the entire time, that would maybe let the Garbage collector do its cleanup. It didn't work.</p>
  <p>I've edited the /etc/dbus-1/system.d/bluetooth.conf to remove any interface that I didn't need</p>
  <pre><code>&lt;policy user=&quot;root&quot;&gt;
      &lt;allow own=&quot;org.bluez&quot;/&gt;
      &lt;allow send_destination=&quot;org.bluez&quot;/&gt;
  &lt;/policy&gt;
  </code></pre>
  <p>That has slow down the RAM build-up but didn't solve the issue.</p>
  <p>I've found a way to inspect which connection has byte waiting and confirmed that it comes from blueZ</p>
  <pre><code>Connection :1.74 with pid 3622 '/usr/libexec/bluetooth/bluetoothd --experimental ' (org.bluez):
          IncomingBytes=1253544
          PeakIncomingBytes=1313072
          OutgoingBytes=0
          PeakOutgoingBytes=210
  </code></pre>
  <p>and lastly, I've found that someone needs to read what is waiting in DBus in order to free the memory. So I've found this : <a href="https://stackoverflow.com/a/60665430/15325057">https://stackoverflow.com/a/60665430/15325057</a></p>
  <p>And I receive the data that BlueZ is sending over but the memory still built-up.</p>
  <p>The only way I know to free up dbus is to reboot linux. which is not ideal.</p>
  <p>I'm coming at the end of what I understand of DBus and that's why I'm here today.
  If you have any insight that could help me to free dbus from BlueZ messages, it would be highly appreciated.</p>
  <p>Thanks in advance</p>
  <p><strong>EDIT</strong> Adding the DBus code i use to read the discovered devices:</p>
  <pre><code>#!/usr/bin/python3
  
  import dbus
  
  BLUEZ_SERVICE_NAME = &quot;org.bluez&quot;
  DBUS_OM_IFACE = &quot;org.freedesktop.DBus.ObjectManager&quot;
  DEVICES_IFACE = &quot;org.bluez.Device1&quot;
  
  def main_loop(subproc):
      devinfo = None
      objects = None
  
      dbussys = dbus.SystemBus()
      dbusconnection = dbussys.get_object(BLUEZ_SERVICE_NAME, &quot;/&quot;)
      bluezInterface = dbus.Interface(dbusconnection, DBUS_OM_IFACE)
      
          
      while True:
          try:
              objects = bluezInterface.GetManagedObjects()
          except dbus.DBusException as err:
              print(&quot;dbus Error : &quot; + str(err))
              pass
  
          all_devices = (str(path) for path, interfaces in objects.items() if DEVICES_IFACE in interfaces.keys())
  
          for path, interfaces in objects.items():
              if &quot;org.bluez.Adapter1&quot; not in interfaces.keys():
                  continue
  
              device_list = [d for d in all_devices if d.startswith(path + &quot;/&quot;)]
  
              for dev_path in device_list:
                  properties = objects[dev_path][DEVICES_IFACE]
                  
                  if &quot;ServiceData&quot; in properties.keys() and &quot;Name&quot; in properties.keys() and &quot;RSSI&quot; in properties.keys():
                      #[... Do someting...] 
  </code></pre>