4

is there a recommended limit for the images in Cocos2d, whether there are too big and take too much memory? Are there some rules, in dimensions or in Kb, to avoid slowing the game down? (for the background image, or the graphics of my characters (even if i use a batch node)?)

Thanks for your answer

Paul
  • 6,108
  • 14
  • 72
  • 128

3 Answers3

10

First of all, memory usage has very, very, very little to do with performance. You can fill up the entire memory with textures, the game won't care. It's when you render them where there will be a difference. And then it only matters how much of the screen area you're filling with textures, how heavily they're overlayed, batched, rotated, scaled, shaded and alpha-blended. Those are the main factors in texture rendering performance. Memory usage plays a very insignificant role.

You may be interested in the cocos2d sprite-batch performance test I did and the general cocos2d performance analysis. Both come with test projects.

As for the maximum texture sizes have a look at the table from my Learn Cocos2D book: enter image description here

Note that iPhone and iPhone 3G devices have a 24 MB texture memory limit. 3rd generation (iPhone 3GS) and newer devices don't have that limit anymore. Also keep in mind that while a device may have 256 MB of memory installed, significantly less memory will be available for use by apps.

For example, on the iPad (1st gen) it is recommended not to use more than 100 MB of memory, with a maximum available memory peaking at around 125 MB and memory warning starting to show as early as around 80-90 MB memory usage.

With iOS 5.1 Apple also increased the maximum texture size of the iPad 2. The safest and most commonly usable texture size is 2048x2048 for Retina textures, and 1024x1024 for standard resolution textures.

Not in the table are iPod touch devices because they're practically identical to the iPhone models of the same generation, but not as easily identifiable. For example the iPod touch 3rd generation includes devices with 8, 16 and 32GB of flash memory, but the 8GB model is actually 2nd generation hardware.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • thanks for your answer, your blog looks really great, i'll check the articles! Thank you very much! – Paul May 04 '12 at 18:20
  • 1
    hi, can i ask you one question? i heard about normal/selected/disabled images, for each device... what do you do when you create all your images for each device? do you create a `-hd` image and a standard image for each normal/selected/disabled version of your image ? Thanks again – Paul May 09 '12 at 01:06
  • @Paul have you solved this? I had asked a similar question (http://stackoverflow.com/questions/10232539/how-to-approach-hd-files) as well as another question where I was suggested by LearnCocos2D to twak the CCFileUtils loader to stop forcing it to require both hd and non hd files.. hope it helps. – mm24 Jun 18 '12 at 08:55
1

The dimensional size of images and textures depends on the device you are supporting. Older devices supported small layers, I think 2048x2048 in size. I don't think such limitation exists on current devices.

For large images, you definitely want to use batch nodes as they have been tested to demonstrate the largest performance gain when dealing with large images. Though it is a good idea to use them for as much as possible in general.

As for how much you can load, it really depends on the device. The new iPad has 1 GB of memory and is designed to have much more available memory for large images. A first-gen iPad has 1/4 this amount of memory, and in my experience I start to see an app crash when it gets around 100 MB of memory used (as confirmed using Instruments).

The trick is to use only as much memory as you need for the current app's operation, then release it when you move to a new scene or new set of images/sprites/textures. You could for example have very large tiled textures where only the tiles nearest the viewport are loaded into memory. You could technically have an infinite sized view that stretches forever if you remove from memory those parts of the view that are not visible onscreen.

And of course when dealing with lots of resources, make sure your app delegate responds appropriately to its memory warnings.

johnbakers
  • 24,158
  • 24
  • 130
  • 258
0

As per my knowledge.. A batch node of size 1024x1024 takes around 4 MB of space which is only texture memory.. And an application has maximum limit of 24 MB. So game slows down as you reach this 24 MB space and crashes after that. To avoid slowness I used maximum of 4 Batch Nodes at one time i.e.16 MB. Rest 8 MB was left for variables and other data. Before using more batch node I used to clean memory and remove unused batch nodes.. I don't know about memory limit in 4s but in case of iPhone 4 this was what I learnt. Using this logic in mind I used to run my game smoothly.

Nikhil Aneja
  • 1,259
  • 8
  • 13
  • why do you say that an application has a memory limit of 24 MB? The limit is determined by the device, and modern devices have more memory. Using Instruments, I can see that an app I am building right now uses around 80 MB of memory while it is running. – johnbakers May 01 '12 at 07:56
  • That is why I mentioned 24 MB for iPhone 4. I don't know about 4S.. More Over its the texture memory that I learnt. The memory in the instrument reached even 150 MB.. I think the memory in instrument is different from memory actually used. Rest I am not really aware of memory allocation on instrument. – Nikhil Aneja May 01 '12 at 09:40
  • I'm quite sure this is not correct information. A 24 MB limit on iPhone 4? I don't think so. You can run apps with 4x this memory usage on iPhone 4 with no problem. And Instruments does correctly report memory usage, otherwise it wouldn't be a useful tool. – johnbakers May 01 '12 at 10:10
  • For cocos2d texture memoryBytes = width * height * bitsPerPixel/8; So for a 32 bit 1024x1024 file will take around 4 MB of texture memory... Check for this link and 2nd answer.. http://stackoverflow.com/questions/457568/iphone-development-memory-limitation-for-iphone-application – Nikhil Aneja May 01 '12 at 10:21
  • 2
    The 24 MB texture memory limit exists only on 1st & 2nd generation devices. The iPhone 3GS and newer have no such limit, they're only limited by available memory. – CodeSmile May 02 '12 at 20:11