2

I realise if I remove subviews accordingly from my application, the memory management seems better ( I have a lot of subviews inside my application).

So my question is it is a must to remove all subviews that you have created or its optional (when you have very little subviews so it doesn't affect the memory usage much)?

Marlon
  • 19,924
  • 12
  • 70
  • 101

3 Answers3

3

You are seeing a noticeable jump in interface response because subviews do in fact consume quite a bit of memory (UIViews are very expensive compared to their underlying CALayers), and as such, calling -removeSubview not only unloads stress from the GPU, but also frees up more memory as the subview is usually released afterwards.

But to answer your question: No. Once a view has passed out of context, or removed from the subview tree, it's own subview tree is broken, the views are released in an ARC environment, then destroyed for you. Even without ARC, it's unnecessary.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
2

It is not a must, since when the super view that is holding view is removed all its subviews are removed too, you really dont need to worry about removing views,

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
0

I have found that assigning a tag for each subview I create allows me to remove it at some future point before the superview is removed, makes it simple to bring in and out elements according to their tag value.
Every object on a view is a subview, so keeping tags on them seems sensible for some applications when graphics are used in a simple way.

Lets say you want to draw a simple horizontal line:

part of a function to draw a horizontal line:

CGFloat lineWidth=8;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(oxx,oyy-(lineWidth/2),pxx+(lineWidth/2),pyy)];
lineView.backgroundColor = colours;
lineView.tag=5;
[self.view addSubview: lineView]; ...

then some time later, to remove some tagged subviews:

 for (UIView *subview in [self.view subviews]) {
    if (subview.tag == 101 || subview.tag == 102 ||subview.tag == 103 ||subview.tag == 104 || subview.tag == 5) {
        [subview removeFromSuperview];
    }
}
JoshDM
  • 4,939
  • 7
  • 43
  • 72