title: How can I clear memory being used by a UICollectionView after changing the data source?
body: <p>I have a UICollectionView that has a few different data sources, each one is an array of UIViews. But I notice that when I change the data source and reload the collection view, the memory usage doesn't stay similar or even go down, it increases.</p>
  
  <p>So my question is how can I clear that memory being used? I have tried setting the data source to an empty array and calling <code>collectionView.reloadData()</code>, but that still did not clear the memory.</p>
  
  <p>Thanks!</p>
  
  <p>Code i'm using:</p>
  
  <pre><code>
  var allThumbArrays = [
          templatesSection1Small,
          templatesSection2Small,
          templatesSection3Small
      ]
  
  
  class sectionsMenuLabels {
  
      static func section1Button() -&gt; UIButton {
          let myButton = UIButton()
          myButton.setTitle("One", for: .normal)
          myButton.setTitleColor(colours.darkbg, for: .normal)
          myButton.isUserInteractionEnabled = false
          return myButton
      }
  
      static func section2Button() -&gt; UIButton {
          let myButton = UIButton()
          myButton.setTitle("Two", for: .normal)
          myButton.setTitleColor(colours.darkbg, for: .normal)
          myButton.isUserInteractionEnabled = false
          return myButton
      }
  
      static func section3Button() -&gt; UIButton {
          let myButton = UIButton()
          myButton.setTitle("Three", for: .normal)
          myButton.setTitleColor(colours.darkbg, for: .normal)
          myButton.isUserInteractionEnabled = false
          return myButton
      }
  }
  
  let sectionMenuButtonsArray: [UIButton] = [
      sectionsMenuLabels.section1Button(),
      sectionsMenuLabels.section2Button(),
      sectionsMenuLabels.section3Button()
  ]
  
  
  let tWidth = ((UIScreen.main.bounds.width - 20) / 3) - 15
  let tHeight = (tWidth / 9) * 16
  let tCollectionWidth = UIScreen.main.bounds.width - 20
  
  
  static func templatesSection() -&gt; UICollectionView {
          let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
          layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 60, right: 10)
          layout.itemSize = CGSize(width: tWidth, height: tHeight)
          let myCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: tCollectionWidth, height: UIScreen.main.bounds.height), collectionViewLayout: layout)
  
          return myCollectionView
      }
  
  
         static func sectionsMenuView() -&gt; UICollectionView {
          let sectionsMenuLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
          sectionsMenuLayout.scrollDirection = .horizontal
          sectionsMenuLayout.sectionInset = UIEdgeInsets(top: 5, left: 15, bottom: 0, right: 15)
          sectionsMenuLayout.itemSize = CGSize(width: 110, height: 25)
  
          sectionsMenuLayout.minimumInteritemSpacing = 10
  
          let sectionsMenu = UICollectionView(frame: CGRect(x: 0, y: 0, width: (screenSizes.screenWidth - 20), height: 35), collectionViewLayout: sectionsMenuLayout)
  
          return sectionsMenu
      }
  
  
  //View controller File
  
  class TemplatesViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
  
      let sectionsMenuView = sectionsMenuView()
      let templatesSection = templatesSection()
  
      let currentSectionShownNumber = 0
      var thumbsToUse: [UIView] = []
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          //MARK: sections menu collection view
          sectionsMenuView.dataSource = self
          sectionsMenuView.delegate = self
          sectionsMenuView.backgroundColor = UIColor.clear
          sectionsMenuView.register(sectionsMenuCell.self, forCellWithReuseIdentifier: "SectionsMenuCell")
          self.view.addSubview(sectionsMenuView)
  
          sectionsMenuView.translatesAutoresizingMaskIntoConstraints = false
          sectionsMenuView.widthAnchor.constraint(equalToConstant: screenSizes.screenWidth).isActive = true
          sectionsMenuView.heightAnchor.constraint(equalToConstant: 40).isActive = true
          sectionsMenuView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
          sectionsMenuView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 40).isActive = true
  
          //MARK: templates section collection view
          templatesSection.dataSource = self
          templatesSection.delegate = self
          templatesSection.backgroundColor = UIColor.clear
          templatesSection.register(TemplateViewCell.self, forCellWithReuseIdentifier: "SectionCell")
          self.view.addSubview(templatesSection)
  
          templatesSection.translatesAutoresizingMaskIntoConstraints = false
          templatesSection.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
          templatesSection.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
          templatesSection.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 20).isActive = true
  
          templatesSection.heightAnchor.constraint(equalToConstant: 150).isActive = true
  
  
      }
  
  
  
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
  
          if collectionView == self.sectionsMenuView {
  
              return sectionMenuButtonsArray.count
  
          } else {
  
              return thumbsToUse.count
          }
  
      }
  
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
          if collectionView == self.sectionsMenuView {
  
              let sectionsMenuCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SectionsMenuCell", for: indexPath as IndexPath)
  
              sectionsMenuCell.addSubview(sectionMenuButtonsArray[indexPath.row])
  
              return sectionsMenuCell
  
  
          } else {
  
  
              let section1Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SectionCell", for: indexPath as IndexPath)
  
  
              //thumbs array
              thumbsToUse = allThumbArrays[currentSectionShownNumber]
  
              //add subview
              let viewToAddToCell = (thumbsToUse[indexPath.row])
  
              sectionCell.addSubview(viewToAddToCell)
  
              return section1Cell
  
          }
  
  
  
      }
  
  
  
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  
          if collectionView == self.sectionsMenuView {
  
  
  
              currentSectionShownNumber = indexPath.row
  
              thumbsToUse.removeAll()
  
              templatesSection.reloadData()
  
  
              thumbsToUse = allThumbArrays[currentSectionShownNumber]
  
              templatesSection.reloadData()
  
  
  
          } else {
  
              chosenTemplateNumber = indexPath.row
  
              chosenTemplate = thumbsToUse[chosenTemplateNumber]
  
          }
  
      }
  
  
  }
  
  </code></pre>