NSMutableArray 和 arrayWithCapacity 区别 NSMutableArray -init vs. +arrayWithCapacity:

http://stackoverflow.com/questions/1730706/nsmutablearray-init-vs-arraywithcapacity



The first creates a mutable array without specifying a capacity. This causes the array to have to grow when you add items. Internally, this is probably heavily optimized to occur "chunks at a time" but it's still necessary to grow the array and allocate more space when adding items.

The second gives the array a hint (you're probably going to need "this much" room) to avoid the overhead of growing the array when adding a known number of objects. Of course it will still grow larger if needed (as if you hadn't specified a capacity). You should use this approach if you already know the count ahead of time. It's faster with a large count.

This has a downside if you haven't measured before optimizing, however: If you're creating a mutable array with a very high capacity but not always using that capacity, you incur the penalty of allocating all that space for nothing.

Also, you don't autorelease B (as you commented out) because you didn't create the mutable array with init - you used a convenience method which did it itself, which means you're not responsible for releasing it. As I mentioned in a comment to another answer to your question, you can also create the array with:

[[NSMutableArray alloc] initWithCapacity:capacity];

... then release it when ready. This gives you greater control over memory usage than using the autorelease pool, which is an important consideration on the iPhone platform.

Remember, though: measure first, then optimize if necessary.

share|improve this answer

edited Nov 13 '09 at 17:46



answered Nov 13 '09 at 17:27


Joshua Nozzi

48.5k10101101


add a comment



up vote

1

down vote

Mutable objects still need to allocate space so that will allocate a default amount for say 10 objects. If you add an 11th, the mutable array will have to allocate new memory, copy the items over to the new memory, and free the old memory.

This is normally so fast you won't even notice but it could slow it down. Creating the mutable array with a size creates the array of the specified size initially so hopefully less resizing will occur.

share|improve this answer

answered Nov 13 '09 at 17:29


ACBurk

3,73822649


add a comment

up vote

1

down vote

In the first example, you must manage the memory of the array, since you create it using +alloc and -init (which is why you need to send -autorelease to it).

In the second example, you do not have to manage the memory of the array, since it is returned to you autoreleased (because you created it using a convenience method). Also since you specify the desired size of the array up front, it is likely to be more efficient.

If you want to return an autoreleased array, then the second option would probably be more preferable, since +arrayWithCapacity: will return an already autoreleased array. Also since the array returned to you is already autoreleased, you do not have to send -autorelease to it yourself.

If you have any more concerns with memory management, then reading the Apple Memory Management Guidelines is a must.

share|improve this answer

edited Nov 13 '09 at 17:35



answered Nov 13 '09 at 17:29


Alex Rozanski

32.1k95465


add a comment

up vote

0

down vote

I'd use B:

The pluses I see with it are

  • The array will not need to resize as it grows larger
  • arrayWithCapacity: autoreleases for you so you don't have to, this improves code readability imho

I hope that helps.

share|improve this answer

answered Nov 13 '09 at 17:30


Bryan McLemore

4,9591729


  

 

Though in situations where you might want more control (over how much memory you're using), you can still create it with [[NSMutableArray alloc] initWithCapacity:c], then release it when you're ready. – Joshua Nozzi Nov 13 '09 at 17:33

  

 

Just to clarify my previous comment - this is an important consideration on the iPhone, which I mention because of the "iphone" tag. :-) – Joshua Nozzi Nov 13 '09 at 17:42

  

 

Absolutely true. I didn't notice the tag, and that may be a serious consideration assuming this is a large array. However, the memory management guidelines are there for a reason. Consistent usage is very important, and should only be broken if you absolutely know why and there's not a cleaner way. – Bryan McLemore Nov 13 '09 at 17:54

  

 

I'm not sure I understand why that's a "however". :-D Whether or not you use an autorelease pool or hint an array's capacity doesn't imply or necessitate breaking memory management rules. Could you clarify what you meant? – Joshua Nozzi Nov 13 '09 at 17:58

  

 

While I'd definitely bow to your experience, I was referring more to breaking them by creating an object and then returning it without it being autoreleased. If you acquired it from the function and then forgot to release it at all, a reasonably common mistake, you'd have a potentially serious leak. Indeed, having a function like a who didn't autorelease at the end would result in a clang warning for potential memory leaks. Sorry for not responding earlier was at lunch. – Bryan McLemore Nov 13 '09 at 18:57

add a comment

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值