2

The project I'm working on requires me to temporarily store hundreds and sometimes thousands of entries in a buffer. The easy way is to store each entry in an NSDictionary and all the entries in an NSArray. Each NSDictionary contains about a dozen objects (NSStrings and NSNumbers). During the entire operation, the NSArray with its dictionaries remains in memory hence my question.

Is this an expensive operation in terms of memory usage and what is a good way to test this?

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Bart Jacobs
  • 9,022
  • 7
  • 47
  • 88

2 Answers2

2

Instruments contains a memory monitoring module. In the bottom-left corner of instruments, click on the gear icon, then choose Add Instrument > Memory Monitory. Apple's documentation should help you understand how to monitor memory with Instruments. See also this question.

In my experience, NSDictionary and NSArray are both fairly efficient in terms of memory usage. I have written several apps that store thousands of keys/values from .csv or .xml files, and there's a fairly linear increase in memory usage as the NSDictionary is filled up. My advice is to use the Instruments profiler in some corner cases if you can built unit tests for them.

I'm not sure I understand why you're storing the entries in both the NSDictionary and the NSArray, though.

One thing you may want to consider if you're reaching upper bounds on memory usage is to convert the entries into a SQLite database, and then index the columns you want to do lookup on.

EDIT: Be sure to check out this question if you want to go deep in understanding iPhone memory consumption.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
retrodrone
  • 5,850
  • 9
  • 39
  • 65
  • Thanks for your response. I have been using Instruments in the past (and prior to posting this question). Unfortunately, it isn't the easiest tool to work with (or interpret the results it produces), but it is indeed best suited for this. – Bart Jacobs Jun 07 '11 at 01:49
  • If you have an apple developer account check out [last year's WWDC](http://developer.apple.com/videos/wwdc/2010/) about instruments and optimizing memory on ios. It is really worth seeing if you which to quickly understand how instruments are working. – retrodrone Jun 07 '11 at 01:54
1

Apple's collection classes are more efficient than you or I would create, I wouldn't worry about a dictionary with thousands of small entries, keep in mind that the values aren't copied when added to a dictionary, but the keys are. That being said only keep in memory what you need to.

Grady Player
  • 14,399
  • 2
  • 48
  • 76