Sunday, May 20, 2007

NSKeyedArchiver is wasting memory

If your program has to handle a large amount of data, remember that NSKeyedArchiver's
-archiveRootObject:toFile: first encodes your document into an instance of NSData and then stores content data to disk using -writeToFile:options:error:.
For reference see according documentation of -archiveRootObject:toFile:.
That means saving a 2GB document to disk may consume n GB of RAM (2GB document + n GB as encoded data in an instance of NSData :-)
Memory consumption of course depends on output format you set to the archiver.

NSKeyedUnarchiver behaves similar:
First the data will be read from disk into an instance of NSData and then it will be decoded from there.

I really miss a direct to disk variant of coding/encoding.
I did not digged deeply into CoreData in combination with SQLite. Maybe this is something worth to take at look at, as theoretically SQLite should never need to have the whole graph in memory. But for an existing project (or if your project should be downward compatible to Panther) it's hard to accept.

No comments: