title: Releasing unused pages in UIPageViewController
body: <p>I am using a separate <code>.h</code>, <code>.m</code>, and <code>.xib</code> files for each <code>UIViewController</code> based page of a <code>UIPageViewController</code> based picture book.  Each page is loaded with animations, music, etc. and takes about 4MB of memory. In Instruments, the free memory drops down about 4MB as each page is loaded.  This memory is never released as the pages are turned.  It eventually gives memory warnings.  <code>UIPageViewController</code> seems to keep each page it instantiates in memory and won't unload it.  So when pages are turned fast, the app crashes.</p>
  
  <p>I would like to be able to unload all pages except the 3 needed by <code>UIPageViewController</code> - the previous, current, and next pages.  How can I unload undesired pages since they were instantiated by <code>UIPageViewController</code>. </p>
  
  <p>Below is the Array of the pages that <code>UIPageViewController</code> pulls from.  All of the pages (Page1, Page2, etc.) basically just load image files, provide basic animation, and have music.</p>
  
  <pre><code>    //ARRAY OF PAGES    
  pageArray = [[NSArray alloc] initWithObjects:
          (id)[[Page1 alloc] initWithNibName:nil bundle:nil],
              [[Page2 alloc] initWithNibName:nil bundle:nil],    
              [[Page3 alloc] initWithNibName:nil bundle:nil],    
              [[Page4 alloc] initWithNibName:nil bundle:nil],    
              [[Page5 alloc] initWithNibName:nil bundle:nil],    
              [[Page6 alloc] initWithNibName:nil bundle:nil], 
              [[Page7 alloc] initWithNibName:nil bundle:nil],
              [[Page8 alloc] initWithNibName:nil bundle:nil],
               // continues all the way up to page 47
               [[Page47 alloc] initWithNibName:nil bundle:nil],
               nil];
  </code></pre>
  
  <p>I've left out the standard initialization for <code>UIPageViewController</code>.  It uses "<code>nextPageNumber</code>" to pull the right page from the <code>pageArray</code> above to create a new page object.</p>
  
  <pre><code>-(void)turnPageForward{
  
  [pageController setViewControllers:[NSArray arrayWithObject:[pageArray objectAtIndex:nextPageNumber]]
                           direction:UIPageViewControllerNavigationDirectionForward
                            animated:YES completion:^(BOOL finished){
                            }];
  </code></pre>
  
  <p>}</p>
  
  <p>I have tried creating an object "<code>pageIndex</code>" (see below) that is set to nil after providing it to the <code>pageController</code>.  It didn't work.  The page still took up memory well after the pages had advanced.</p>
  
  <pre><code>//PROGRAM PAGE FORWARD
  </code></pre>
  
  <p>-(void)turnPageForward{ </p>
  
  <pre><code>UIViewController * pageIndex =[pageArray objectAtIndex:nextPageNumber];  //nextPageNumber is the next page to load
  
  [pageController setViewControllers:[NSArray arrayWithObject:pageIndex]
                           direction:UIPageViewControllerNavigationDirectionForward 
                            animated:YES completion:^(BOOL finished){
                            }];                                  
  pageIndex = nil;                            
  </code></pre>
  
  <p>}</p>
  
  <p>I've looked through stackoverflow for posts using the same way of supplying pages to <code>UIPageViewController</code>, but haven't found anything close.  The closest was "<em>ARC not releasing memory when going “back” in navigation controller</em>" but doesn't set the view controllers the same way. </p>
  
  <p>I've tried to set the undesired pages to nil so ARC can remove them with no luck.  Any suggestions or alternate paths I should try?  I like the page curl effect and have not been able to find a good one elsewhere that does horizontal page curls. </p>
  
  <p>Thanks!  Eric</p>