Game using large amounts of memory

Discussion in 'iOS Programming' started by Feltzem, Nov 9, 2012.

  1. macrumors regular

    Sep 20, 2009
    I'm developing an iOS game that has shooting and reload animations for weapons. Per gun, the total of the images that make up the animation is only about 10MB, yet when they load on the device (retina iPad) the app increases in memory use by about 400MB. Why is this?

    When the shooting view loads, it loads the images into two arrays (fire and reload), and plays the animation over in 0.1 seconds before it loads so that there is no delay when shooting or reloading. There is little else in the view that would be using large amounts of memory.

    What would you recommend I do to decrease the amount of memory the game is using?
  2. Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
  3. thread starter macrumors regular

    Sep 20, 2009
    Like this: ('rev' is the UIImageView the animation plays in, gunanimload is the array)

    - (void)viewDidLoad
        [super viewDidLoad];
    myQueue = dispatch_queue_create("com.dubzem.shooting", NULL);
        dispatch_async(myQueue, ^{
        gunanimload = [NSArray arrayWithObjects:
    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rev0001" ofType:@"png"]], 
    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rev0002" ofType:@"png"]],
     [B](etc x 30)[/B] nil];
    rev.animationImages = gunanimload;
        rev.animationDuration = 0.10;
        rev.animationRepeatCount = 1;
        [rev startAnimating];});
    And when the Fire button is tapped:

    type.animationImages  = gunanimload;
        type.animationDuration = 0.5;
        type.animationRepeatCount = 1;
        [type startAnimating];
    Reload is the same, except with 41 images in the array.
  4. thread starter macrumors regular

    Sep 20, 2009
    Is this an efficient way to load and play a PNG animation? I'm wondering why it's using so much memory even though the images are small in size.
  5. macrumors regular

    Sep 11, 2008
    I warmly recommend you use Instruments. If you really have more than ten times greater memory usage than what you'd expected, you should stop guessing and start with the Instruments.

    There you'll see the objects that occupy a lot of memory. 400MB on iOS device is an incredible amount of memory. You'll see malloc blocks too, and guessing where they come from may take a bit more time but their size should point you in the right direction.
  6. macrumors 68030


    Sep 2, 2008
    The amount of memory used by an image is wxhx4. It's not the same as the size of the image on disk.

Share This Page