title: How to reduce memory usage on UICollectionView with large quantity of items?
body: <p>I'm trying to use a UICollectionView instead of a UITableView to display a very large number of items (>15000) and it seems the UICollectionView pre-allocates a pixel buffer for the entire contentView size required by the collection.  Depending on the item sizes, the simulator shows up to 6.75GB of memory required.</p>
  
  <p>I was hoping the collection view, based on its protocol being very similar to UITableView, wouldn't allocate any pixel buffer and rely solely on the cells' backing/rendering.</p>
  
  <p>I'm using a storyboard file to define the collection view and cell, both have Opaque = false. I've looked at numerous articles on Stack Overflow, most have to do with memory leaks so I'm a little stumped as to how to fix the issue.</p>
  
  <p>For the curious, here's the entire code base (other than the Storyboard):</p>
  
  <p>MyCollectionViewCell.swift</p>
  
  <pre><code>import UIKit    
  class MyCollectionViewCell: UICollectionViewCell {
      static let identifier = "myCellIdentifier"
  }
  </code></pre>
  
  <p>UIColor+Random.swift</p>
  
  <pre><code>import UIKit
  extension UIColor {
      static func randomColor() -&gt; UIColor {
          let red = CGFloat(drand48())
          let green = CGFloat(drand48())
          let blue = CGFloat(drand48())
          return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
      }
  }
  </code></pre>
  
  <p>ViewController.swift</p>
  
  <pre><code>import UIKit
  class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
  
      func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
          return 10000
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 30000
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
          cell.backgroundColor = UIColor.randomColor()
          return cell
      }
  
  
      @IBOutlet weak var collectionView: UICollectionView!
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KEdVQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KEdVQ.png" alt="Storyboard"></a></p>
  
  <p>Memory usage under certain use cases:
  <a href="https://i.stack.imgur.com/e4Iww.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e4Iww.png" alt="Number of sections greatly affecting memory usage"></a></p>