yashwanth77
yashwanth77

Reputation: 1860

How to release memory of child view controllers

I have a page view controller in which i have four view controller.My question is when i go to second view controller i need to release memory of first,third and fourth view controller similarly for other three view controller.How to find whether it is allocating or deallocating memory for view controllers using instruments.I have tried using dealloc but the method is not getting called and i am using arc.I have my code as follows

-(NewsViewController *)news

{

if (!news) {

UIStoryboard *storyboard = self.storyboard;
news = [storyboard instantiateViewControllerWithIdentifier:@"News"];
}

return news;

}

-(PriceViewController *)price 
{

  if (!price) {

    UIStoryboard *storyboard = self.storyboard;
    price = [storyboard instantiateViewControllerWithIdentifier:@"price"];

  }
return price;

}

- (CommentryViewController *)commentry {

if (!commentry) {
    UIStoryboard *storyboard = self.storyboard;
    commentry = [storyboard instantiateViewControllerWithIdentifier:@"commentry"];
}

return commentry;
}

-(WatchlistViewController *)watchlist {

if (!watchlist) {
    UIStoryboard *storyboard = self.storyboard;
    watchlist = [storyboard instantiateViewControllerWithIdentifier:@"watchlist"];
}

return watchlist;
}


- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

UIViewController *nextViewController = nil;

if (viewController == self.news) {
    nextViewController = self.commentry;


}
else if(viewController == self.commentry) {
            titleView.text = @"Commentry";
    [titleView sizeToFit];

    //self.title=@"Commentry";
    nextViewController = self.price;

}
else if(viewController == self.price) {

    titleView.text = @"Price";
    [titleView sizeToFit];
    nextViewController = self.watchlist;


}
else if(viewController == self.watchlist) {
    titleView.text = @"WatchList";
    [titleView sizeToFit];

}

return nextViewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

UIViewController *prevViewController = nil;

if (viewController==self.watchlist) {
    titleView.text = @"WatchList";
    prevViewController = self.price;


}
else if (viewController == self.price) {
        titleView.text = @"Price";
    prevViewController = self.commentry;
       }
else if (viewController == self.commentry) {
    titleView.text = @"Commentry";
    prevViewController = self.news;


}
else if(viewController == self.news) {

    titleView.text = @"News";
}

return prevViewController;
}

Upvotes: 0

Views: 1392

Answers (1)

Andrea
Andrea

Reputation: 26385

In a UIPageViewController if you call -viewControllers, you should see all the view controllers loaded in memory and if this array is filled with some you should not force a dealloc of them, because page view controller manages the memory pretty well removing unneeded view controllers from that array. That means that if no one else has ownership on them the will be dealloced and you should see a a decrease in the Memory Allocation tool.
View controllers has their own way to handle thier memory occupation(rules are always the same of the language, but there is an inner mechanism that helps to keep memory low) when in memory pressure, that is because view controllers do not occupy much memory by themselves, but the resources loaded with them do.
Memory management behavior in VC has changed a lot since iOS 6 (and I'm going to talk only about that), when the system is in need of memory, not visible VCs mark their views backing layer as dirty ready to be released, usually this frees up a lot of memory, but if in you view controller you create and take the ownership on a lot of objects, this memory can never be freed, unless you do in the method -didReceiveMemoryWarning, or you get rid of them when you have finished using them. You should never call dealloc and in ARC is also forbidden, if you have a huge memory grow in memory, you need to check how you manage the memory of those objects.
I make an example to be clear, let's suppose that in you page VC you load other VCs with full screen images, if your load them using +imageNamed those images will finish in a cache and even if your VC is correctly dealloced the image occupation will remain.

Upvotes: 1

Related Questions