iPhone memory size and Obj-C sizeof()

Discussion in 'iOS Programming' started by mlady, Sep 18, 2008.

  1. mlady macrumors member

    Sep 18, 2008
    Hi there,
    I have two questions if I may:

    1) I am making a 2d view from top racing game using OpenGL. And I want to put there really big background. Unfortunately it doesn't work on iPhone. So my question is how big texture I can use?

    2) This one is related to the first one. Is there any way in Obj-C to determine the size of an object? And how much memory I have used?

    Thanks. If you need more info, bit of code perhaps, just ask I can provide that.

    Thank you again.
  2. mlady thread starter macrumors member

    Sep 18, 2008
    I think I have figured out the first problem. It can be up to 1024x1024 pixels. So I can put together the background from these pieces.

    But I am still interested how can I determine size in bytes of an object?
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    There's no method that will allow you to determine the full memory footprint of an arbitrary object. You can use the sizeof operator to tell the size of the struct that an object represents. However, many objects will also have pointers to other blocks of memory. These are private and there's no language support to determine them. Also, various classes may have memory that's just separate from the individual instance. For instance all NSObjects have a retain count. Where is this stored? There's no instance variable that holds it. Also, various classes will preallocate memory for a cache so one api call may allocate a large block and another identical api call may allocate no memory.

    What you can do is use the ObjectAlloc tool. It is possible to view all memory allocations done by your app with that tool. So it is conceivable that you could look at the memory allocations that occur when you create a given object or call a given OS API. You can view the total memory footprint of your app over time. This is probably the best you can do but it also has limitations.

Share This Page