Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte size of an NSDictionary

This may sound like a completely stupid question, but how can I get the size in bytes of an NSDictionary? Can I convert it to NSData, and then get the length of that?

Help!

like image 989
mootymoots Avatar asked Mar 05 '11 22:03

mootymoots


People also ask

What is NSDictionary?

An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.

What is the difference between NSMapTable vs NSDictionary?

NSDictionary / NSMutableDictionary copies keys, and holds strong references to values. NSMapTable is mutable, without an immutable counterpart. NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated.

How do you set a value in NSDictionary?

You have to convert NSDictionary to NSMutableDictionary . You have to user NSMutableDictionary in place of the NSDictionary . After that you can able to change value in NSMutableDictionary . Save this answer.

Does NSDictionary retain objects?

An NSDictionary will retain it's objects, and copy it's keys. Here are some effects this has had on code I've worked on. Sometimes you get the same object you put in, sometimes not. Immutable objects are optimized to return themselves as a copy .


2 Answers

You should say more about why you care about the size of the dictionary in bytes, because the answer might be different depending.

In general, the "size" of an NSDictionary's footprint in memory is not something you can see or care about. It abstracts its storage mechanism from the programmer, and uses some form of overhead beyond the actual data it's storing.

You can, however, serialize the dictionary to NSData. If the contents of the dictionary are only "primitive" types like NSNumber, NSString, NSArray, NSData, NSDictionary, you can use the NSPropertyListSerialization class to turn it into a binary property list, which will be about the most compact byte representation of its contents that you can get:

NSDictionary * myDictionary = /* ... */;
NSData * data = [NSPropertyListSerialization dataFromPropertyList:myDictionary
    format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];    
NSLog(@"size: %d", [data length]);

If it contains other custom objects, you could use NSKeyedArchiver to archive it to an NSData, but this will be significantly larger and requires the cooperation of your custom classes.

like image 159
Ben Zotto Avatar answered Oct 17 '22 23:10

Ben Zotto


You can get the size of any class by calling this:

#import "objc/runtime.h"
int size = class_getInstanceSize([NSDictionary class]);

This will return you the size of the class, but not the actual size occupied by an instantiated object. If you want the size of an instantiated object:

#import "malloc/malloc.h"
int size = malloc_size(myObject);
like image 45
Zoran Simic Avatar answered Oct 18 '22 01:10

Zoran Simic