On iOS 7 and above, older answers no longer work for persistent repositories supported by SQLite, due to SQLite log files. For each persistent storage file, you need to get the size of the SQLite file itself (e.g. Foo.sqlite ) and the size of the log files (e.g. Foo.sqlite-wal and Foo.sqlite-shm ) and add sizes to get the total. This is really important because it is very likely that most of the data is actually in the log files. You can use NSFileManager to get this information for each file.
If you use binary attributes in your model and the option "Allow external storage" is enabled for any of these attributes, it becomes more complex. In this case, you need to find all external storage files and add their sizes. Their location is not documented, but should be in a subdirectory of the directory in which the SQLite file is found. For the SQLite file named Foo.sqlite find the directory named .Foo_SUPPORT/_EXTERNAL_DATA , and the external binaries will be there. Since this is not documented, it is subject to change without notice.
The best approach - if not too late - is to put persistent storage in your own subdirectory. Instead of putting it in the document directory, create a new directory inside the documents and place your SQLite file there. That is, create a directory called data and put Foo.sqlite in it. To add a size, just recursively scan each file in data and add the sizes. Then you will catch everything, even if journaling or external binaries or something changes in future versions of iOS. A good way to scan would be the NSFileManager enumeratorAtPath: or enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: .
Finding the memory size for a particular row of data is a completely different issue, and there is no general solution. There may be the overhead of any instance of the object plus your attributes plus any internal undocumented NSManagedObject (including vars instances and dynamically allocated memory). This is not even a fixed value, since data can be extracted and released on the fly. Adding the dimensions of only your attributes is easy - just skip them and add the size of each (line length, data length, etc.), but this is not a complete image.
Tom harrington
source share