I have created an instance of CALayer using [CALayer layer]
and set its frame to the frame of the screen (e.g. 768 by 1024 points). I have not set the contents of this layer. Will this layer take up extra memory because of the larger size, or does the memory consumption remain the same because it has nothing to draw?

- 107
- 2
- 9
-
1I will post this as a comment since I haven't verified it, but the actual heavy item of any view (or layer) is its *backing store* which is created on demand. It is basically a bitmap for all the pixels in the layer, so if you haven't drawn or used the layer yet, I assume all you have is a few bytes of metadata. This is the way the new view system in iOS 6 works. Instead of viewDidUnload, the backing store is simply purged while leaving the view variables and properties intact. – borrrden Nov 06 '12 at 01:43
3 Answers
A CALayer basically is just a few properties and methods. So the CALayer itself (almost) doesn't cost memory. If you assign an Image to the contents-property of course then this will cost the amount of memory your Image uses in decompressed (CGImage) form.
(Afaik the CALayer only stores an reference to the actual Image so to be precise the CALayer itself still has almost no memory usage)
To check the memory-usage of your App at runtime use the (very cool) Profiling-Tool:
- Build for Profiling
- Under run, choose Profile
- Choose "Memory usage" (or similar, I'm not sitting at my MAC right now)
- Click the red "record"-button on the top-left
enjoy

- 100
- 9
I just measured it by allocating a large number of empty CALayer
's, looks like each takes about 340 bytes of memory. Assigning some ridiculously large frames doesn't change it even a bit.
Layers, however, can consume significant amount of memory and not only because of the image contents, but also due to internal pixel caching it seems. I haven't experimented with this yet, but pretty sure even shape or text layers aren't that small once they start drawing on the screen.

- 11,842
- 9
- 51
- 72
If you want, you can check the free memory before and after create the layer. If you want to do that please check this:

- 1
- 1

- 887
- 7
- 22