Memory usage of load an image with Sprite::create()

memory usage shown with code stack hierarchy

This shows what happened to memory when we load a image file. + memory increase - memory decrease (optional) it means this progress is optional, depend on what format of the image file you are using. 01. Sprite::create() + new Sprite() 02. sprite->initWithFile() 03. textureCache->addImage() + new Image() 04. image->initWithImageFile() 05. FileUtil::getDataFromFile(); + new Data() data[data_size] 06. initWithImageData() 07. unpack the compressed data(zip, gzip)+ unpackData [unzipdata_size] (optional) 08. initWithXXXData() 09. read file + Image::_data[pixel_size] or _data[compressed_size] 10. free unpackData - unpackData 11. free data - Data 12. new Texture2D() + Texture2D() 13. texture2D->initWithWithImage() 14. convertDataToFormat() + convertedData[pixel_size] (optional) 15. initWithData() 16. transfer image data to opengl (+ display card memory size[imageData_size]) 17. delete converedData - convertedData (optional) 18. delete Image - Image -Image::_data 19. return sprite

Memory Usage Analysis Figure

Now let's use Sprite::create() to load a png file(160KB, 480*320, RGB format) and use xcode memory analysis to see what happens: (The numbers means the code line shown at above) All the other format of image file load progress are the same, but if you use a compressed image file that opengles can support(like pvr format file for ios), the Image::_data doesn't need to decompress to the pixel data, so it can shrink the memory usage and process time.

peak value of memory usage

There are two peaks during load an image to create a sprite. 1. at line 09 Memory Useage: Sprite + Image + data[data_size] + unpackData[unzipdata_size] + Image::_data[pixel_size] 2. at line 16 Memory Useage: Sprite + Texture2D + Image + Image::_data[pixel_size] + convertedData[pixel_size] + (display card memory size)

memory usage when load finished

Memory Useage: Sprite + Texture2D + (display card memory size)