title: clearing memory of dynamic object instances sometimes slow, sometimes fast, EXCEL VBA
body: <p>My EXCEL VBA code builds up 1500+ instances of complex objects, always with good speed.
  After all data have been processed my code cleans up, i.e. all objects and collections of them are getting set to NOTHING.
  Sometimes this clean-up is fast (~10s), sometimes slow (>5 minutes).</p>
  
  <p>I never start this code multiple times in the same session, i.e. I always close the workbook (all workbooks) and make sure Excel is closed.<br>
  There are no conditional formattings.<br>
  Screen updating is set to FALSE.<br>
  I use MS Office Professional Plus 2016.</p>
  
  <p>Does anybody have an idea why it sometimes is fast and sometimes slow?</p>
  
  <p>As some code is asked for here's the upper level clean-up loop for the superItems collection:</p>
  
  <pre><code>For idx = 1 To superItems.Count
      Application.statusBar = thisFunction &amp; ": " &amp; superItems.Count - idx &amp; " items left"
      Set superItem = superItems(idx)
      If Not (superItem Is Nothing) Then
          superItem.clear
          Set superItem = Nothing
      End If
      DoEvents
  Next idx
  
  Set superItems = Nothing
  </code></pre>
  
  <p>... and here's the method superItem.clear:
  the superItem object holds an array (pArrTmxxItems) with pointers to item objects.</p>
  
  <pre><code>ub = UBound(pArrTmxxItems)
  For idx = 0 To ub
      Set item = pArrTmxxItems(idx)
      If Not (item Is Nothing) Then
          item.clear
          Set item = Nothing
      End If
  Next idx
  </code></pre>
  
  <p>... item.clear looks like this:
  the item object holds a collection pCAs of CA object instances, and (b) a collection pChildren of its own kind (i.e. aChild is of the same class as item)</p>
  
  <pre><code>If Not (pCAs Is Nothing) Then
      For idx = 1 To pCAs.Count
          Set CA = pCAs(idx)
          If Not (CA Is Nothing) Then Set CA = Nothing            'jp171107
      Next idx
      Set pCAs = Nothing                                          'jp171107
  End If
  
  If Not (pChildren Is Nothing) Then
      For idx = 1 To pChildren.Count
          Set aChild = pChildren.item(idx)
          aChild.clear
          Set aChild = Nothing
      Next idx
      Set pChildren = Nothing                                     'jp171107
  End If
  </code></pre>