Daisy
Daisy

Reputation: 71

calculated app size not showing correct in iOS

My app has some content dumping in document folder periodically. And i have used the below code to calculate my total app consumed memory size. But i see its different from what i see from settings\usage. My code shows very less memory. I am not able to track where i am going wrong in calculating/ or have i missed folder paths.

I have calculated my app size as follows:

NSString *folderPath = [[NSBundle mainBundle] bundlePath];
NSArray *filesArray = [[NSFileManager defaultManager]      subpathsOfDirectoryAtPath:folderPath error:nil];
NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;

while (fileName = [filesEnumerator nextObject]) {
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath: [folderPath stringByAppendingPathComponent:fileName] error:nil];
fileSize += [fileDictionary fileSize];
};

Upvotes: 2

Views: 314

Answers (1)

Sherry
Sherry

Reputation: 11

The above code returns the actual file size. The value shows in the settings/usage shows the allocated size. The allocated size shall be more than the actual size.

That's why your calculated value and the setting/usage values are not matching.

If you need to access the allocated size of a file please use NSURLTotalFileAllocatedSizeKey in NSURL class. There is no methods/ property available in NSFileManager class to get the allocated size [As far as i know].

Please go through details of File Property Keys of the NSURL class.

FYI: We shall get permission error if you try to access the /Library/Caches/Snapshot folder in Home directory of sanboxed app.

This may be helpful to you.

Upvotes: 1

Related Questions