title: Is there a way to deallocate a page in uipageviewcontroller after being swiped in swift ios?
body: <p>I am new to swift iOS and I just wanna ask if there is a way to like remove or deallocate a page after being swiped because it's eating up too much ram every time I swipe to the next page. I am working on 64 pages so if I don't apply deallocation then the app might freeze or crash.</p>
  
  <p>ViewController.swift</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UIPageViewControllerDataSource {
  
  // MARK: - Variables
  private var pageViewController: UIPageViewController?
  
  // Initialize it right away here
  private var contentImages = [
      "CQ print version_Page_1.jpg",
      "CQ print version_Page_2.jpg",
      "CQ print version_Page_3.jpg",
      "CQ print version_Page_4.jpg",
      "CQ print version_Page_5.jpg",
      "CQ print version_Page_6.jpg",
      "CQ print version_Page_7.jpg",
      "CQ print version_Page_8.jpg",
      "CQ print version_Page_9.jpg",
      "CQ print version_Page_10.jpg",
      "CQ print version_Page_11.jpg",
      "CQ print version_Page_12.jpg",
      "CQ print version_Page_13.jpg",
      "CQ print version_Page_14.jpg",
      "CQ print version_Page_15.jpg",
      "CQ print version_Page_16.jpg",
      "CQ print version_Page_17.jpg",
      "CQ print version_Page_18.jpg",
      "CQ print version_Page_19.jpg",
      "CQ print version_Page_20.jpg",
      "CQ print version_Page_21.jpg",
      "CQ print version_Page_22.jpg",
      "CQ print version_Page_23.jpg",
      "CQ print version_Page_24.jpg",
      "CQ print version_Page_25.jpg",
      "CQ print version_Page_26.jpg",
      "CQ print version_Page_27.jpg",
      "CQ print version_Page_28.jpg",
      "CQ print version_Page_29.jpg",
      "CQ print version_Page_30.jpg",
      "CQ print version_Page_31.jpg",
      "CQ print version_Page_32.jpg",
      "CQ print version_Page_33.jpg",
      "CQ print version_Page_34.jpg",
      "CQ print version_Page_35.jpg",
      "CQ print version_Page_36.jpg",
      "CQ print version_Page_37.jpg",
      "CQ print version_Page_38.jpg",
      "CQ print version_Page_39.jpg",
      "CQ print version_Page_40.jpg",
      "CQ print version_Page_41.jpg",
      "CQ print version_Page_42.jpg",
      "CQ print version_Page_43.jpg",
      "CQ print version_Page_44.jpg",
      "CQ print version_Page_45.jpg",
      "CQ print version_Page_46.jpg",
      "CQ print version_Page_47.jpg",
      "CQ print version_Page_48.jpg",
      "CQ print version_Page_49.jpg",
      "CQ print version_Page_50.jpg",
      "CQ print version_Page_51.jpg",
      "CQ print version_Page_52.jpg",
      "CQ print version_Page_53.jpg",
      "CQ print version_Page_54.jpg",
      "CQ print version_Page_55.jpg",
      "CQ print version_Page_56.jpg",
      "CQ print version_Page_57.jpg",
      "CQ print version_Page_58.jpg",
      "CQ print version_Page_59.jpg",
      "CQ print version_Page_60.jpg",
      "CQ print version_Page_61.jpg",
      "CQ print version_Page_62.jpg",
      "CQ print version_Page_63.jpg",
      "CQ print version_Page_64.jpg"];
  
      //var contentImages = [String]()
  
  // MARK: - View Lifecycle
  override func viewDidLoad() {
      super.viewDidLoad()
      createPageViewController()
      setupPageControl()
  }
  
  private func createPageViewController() {
  
      let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
      pageController.dataSource = self
  
      if contentImages.count &gt; 0 {
          let firstController = getItemController(0)!
          let startingViewControllers: NSArray = [firstController]
          pageController.setViewControllers(startingViewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
      }
  
      pageViewController = pageController
      addChildViewController(pageViewController!)
      self.view.addSubview(pageViewController!.view)
      pageViewController!.didMoveToParentViewController(self)
  }
  
  private func setupPageControl() {
      let appearance = UIPageControl.appearance()
      appearance.pageIndicatorTintColor = UIColor.grayColor()
      appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
      appearance.backgroundColor = UIColor.darkGrayColor()
  }
  
  // MARK: - UIPageViewControllerDataSource
  
  func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -&gt; UIViewController? {
  
      let itemController = viewController as! PageItemController
  
      if itemController.itemIndex &gt; 0 {
          return getItemController(itemController.itemIndex-1)
      }
  
      return nil
  }
  
  func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -&gt; UIViewController? {
  
      let itemController = viewController as! PageItemController
  
      if itemController.itemIndex+1 &lt; contentImages.count {
          return getItemController(itemController.itemIndex+1)
      }
  
      return nil
  }
  
  private func getItemController(itemIndex: Int) -&gt; PageItemController? {
  
      if itemIndex &lt; contentImages.count {
          let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as! PageItemController
          pageItemController.itemIndex = itemIndex
          pageItemController.imageName = contentImages[itemIndex]
          return pageItemController
      }
  
      return nil
  }
  
  // MARK: - Page Indicator
  
  func presentationCountForPageViewController(pageViewController: UIPageViewController) -&gt; Int {
      return contentImages.count
  }
  
  func presentationIndexForPageViewController(pageViewController: UIPageViewController) -&gt; Int {
      return contentImages.count
     }
  
  }
  </code></pre>
  
  <p>PageItemController.swift</p>
  
  <pre><code>import UIKit
  
  class PageItemController: UIViewController {
  
  // MARK: - Variables
  var itemIndex: Int = 0
  var imageName: String = "" {
  
      didSet {
  
          if let imageView = contentImageView {
              imageView.image = UIImage(named: imageName)
          }
  
      }
  }
  
  @IBOutlet var contentImageView: UIImageView?
  
  // MARK: - View Lifecycle
  override func viewDidLoad() {
      super.viewDidLoad()
      contentImageView!.image = UIImage(named: imageName)
     }
  }
  </code></pre>