title: Memory wont get cleared from background thread in Swift
body: <p>I have an app that is constantly reading data from my server and updating the data in the UI. It pulls quite a bit of data, builds some data structures, and then passes the information to the UI. The code below shows how I am collecting the data:</p>
  <pre><code>class Server {
      private var units: [Unit] = []
  
      ...
  
      init {
          // I pull data from a firebase realtime database here using the observe method
          // which triggers the callback everytime the data changes
          // 'data' is a big dictionary of dictionarys  which will be sorted into objects
          FirebaseDatabaseHandler.getServerInfo(serverAddress: address, callback: { data in
  
              // Because its a lot of data sorting it is hefty so I do this on a background thread
              // 'updateQueue' is a single static DispatchQueue that I create in AppDelegate for now
              AppDelegate.updateQueue.async {
  
                  // here I create an array of data objects using the JSON I pulled from firebase
                  // then I set the &quot;units&quot; variable of this object and call my update callback
                  // which triggers a UI update on the main thread
                  if let unitData = (data?[&quot;units&quot;] as? [String:Any]) {
                      var unitsArray = [Unit]()
              
                      for key in unitData.keys {
                          unitsArray.append(Unit(address: key.base64Decode, data: unitData[key] as! [String:Any]))
                      }
              
                      self.units = unitsArray
                      self.updateCallback()
                  }
              } 
         })
     }
     
     ...
  </code></pre>
  <p>The above code works fine, however the memory is constantly building and not being properly freed, after running for ~10-20 minutes the app builds up to 2GB of memory and crashes from running out of memory.</p>
  <p>If I get rid of the <code>AppDelegate.updateQueue.async { }</code> and just let this code run on the main thread, the memory DOES get cleared and there are no crashes, the memory stays around 50-200mb, however if I do this the UI is essentially permanently frozen due to how much processing is happening on the main thread</p>
  <p>Ive tried using the debugger to watch the size of my <code>units</code> array and my <code>Server</code> object, but neither grow in size no matter how long the app is running.</p>
  <p>Is there anything I can do to debug this, or any reason why the memory wont get cleared when I run it from a background thread?</p>