I am loading a number of UIViews onto a UIScrollView and am trying to track down why they are using so much memory. After a process of elimination using Instruments I have discovered that setting the background colour of the views increases memory usage by 4 times.
If I don't set the background colour the memory usage sits at around 4.5megs. As soon as I set the background colour to anything redColor or clearColor the memory usage jumps to 17megs.
Here is the code:
ThumbnailView *thumbView = [[ThumbnailView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 225.0f, 320.0f)];
thumbView.tag = aCounter;
thumbView.backgroundColor = [UIColor clearColor];
Does anyone know what could be causing this?
What I am really after is to have the background clear. If there is no way around this, is there another way of setting the background of a UIView to be clear?
All instances of UIView (and classes derived from it) have an associated CALayer object (accessed via the layer property) that provides the UIView's visual appearance. The CALayer can have it's own bitmap, it can share a bitmap with another CALayer object (which is how reflections are done), or it can have no bitmap.
When a UIView acts as a container for other controls, it has no bitmap associated with it's layer, thus it uses very little memory. As soon as you set it's background colour, that backing bitmap has to be created so that there is something to render. If the UIView subclass implements drawRect to draw some graphics into the view, the same thing will happen.
Because a full-screen sized view consumes a lot of memory, when you implement a UIScrollView based solution, you should only load the views that are displayed and the two either side. Don't create loads of them in advance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With