title: QML Memory leak sending XMLHttpRequest
body: <p>Creating <code>new</code> instances of <code>XMLHttpRequest</code> and calling <code>send()</code> results in memory usage that can't be cleared by the garbage collector, nor <code>gc()</code>. Calling <code>delete</code> on the object doesnt' clear the memory neither.</p>
  
  <pre><code>import QtQuick 2.12
  import QtQuick.Window 2.12
  
  Window {
      visible: true
      width: 640
      height: 480
  
      Component.onCompleted: {
          for(var i = 0; i &lt; 100000; i++) {
              console.log("Send request " + i)
  
              var xhttp = new XMLHttpRequest
              xhttp.open('get', 'someurl')
              xhttp.send()
              delete xhttp
          }
  
          gc() //why won't this clean the instances of XMLHttpRequest???
      }
  }
  
  </code></pre>
  
  <p>If I never call <code>xhttp.send()</code> then I don't have any memory leak. Garbage collection kicks in since there's no reference to the <code>var xhttp</code> and the memory is freed. I thought maybe the garbage collector wasn't triggering, but <code>gc()</code> wouldn't clear the memory neither.</p>
  
  <p>This MRE will run 100,000 iterations and holds about 500MB in memory. This can easily hold 5.0GB by changing to <code>i &lt; 1000000</code>.</p>
  
  <p>How do I fix this memory leak and free the memory?</p>
  
  <p>References to similar questions:
  <a href="https://bugreports.qt.io/browse/QTBUG-43005" rel="nofollow noreferrer">QTBUG-43005 (No resolution)</a>
  <a href="https://bugreports.qt.io/browse/QTBUG-50231" rel="nofollow noreferrer">QTBUG-50231 (No resolution)</a></p>
  
  <p>Now documented on <a href="https://bugreports.qt.io/browse/QTBUG-83857" rel="nofollow noreferrer">QTBUG-83857</a></p>
  
  <p><a href="https://i.stack.imgur.com/cTJ9t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cTJ9t.png" alt="enter image description here"></a>
  Here it is, holding 2.0GB of memory. It held it for nearly 4 hours until I killed the program. When I closed the application, after about 60 seconds the whole 2GB memory was freed</p>
  
  <p><strong>An Attempt at using QNetworkAccessManager class</strong></p>
  
  <pre><code>//main.qml
      MyClass {
          id: myNetworkClass
  
          Component.onCompleted: {
              for(var i = 0; i &lt; 10000; i++) {
                  myNetworkClass.doDownload()
              }
          }
      }
  </code></pre>
  
  <pre><code>//myclass.h
  #ifndef MYCLASS_H
  #define MYCLASS_H
  
  #include &lt;QObject&gt;
  #include &lt;QNetworkAccessManager&gt;
  #include &lt;QNetworkRequest&gt;
  #include &lt;QNetworkReply&gt;
  #include &lt;QDebug&gt;
  
  
  class MyClass : public QObject
  {
      Q_OBJECT
  
  public:
      explicit MyClass(QObject *parent = 0):QObject(){
          manager = new QNetworkAccessManager(this);
  
          connect(manager, SIGNAL(finished(QNetworkReply*)),
                  this, SLOT(replyFinished(QNetworkReply*)));
      }
  
  public:
      Q_INVOKABLE void doDownload() {
          manager-&gt;get(QNetworkRequest(QUrl("https://esi.evetech.net/latest/characters/93610700")));
      }
  
  public slots:
      void replyFinished(QNetworkReply *reply) {};
  
  private:
      QNetworkAccessManager *manager;
      int count = 0;
  };
  
  #endif // MYCLASS_H
  </code></pre>
  
  <p>This unfortunately also holds onto memory and doesn't release it, according to htop.</p>