简体   繁体   中英

Limit available memory to iOS app in Xcode

I am curious if it is possible to restrict available memory to iOS app in Xcode while debugging. My application is able to record temporary videos using AVCapture and I would like to test if there is enough memory on the user's iDevice while recording and handle the exception inside didReceiveMemoryWarning . Thanks.

EDIT: I've forgotten to mention that I am using actual device, but I own 64GB version so it can take some time to reach this limit and I expect that the end users will be younger people with 8GB iPhones.

If you want to check available Ram (didReceiveMemoryWarning) The answer is No. iOS determinate by itself available memory. According to Apple documentation, you need to use actual device.

Even though iOS Simulator is a useful tool, never make it the only way you test an app. Because iOS Simulator is an app running on a Mac, it has access to the computer's memory, which is much greater than the memory found on a device. As a result of the increased memory, iOS Simulator is not an accurate test of an app's memory usage. For this same reason, always test the performance of your app's user interface on a device. In iOS Simulator, your app's user interface may appear to run both faster and smoother than on a device.

But, if you want to check free space on device - it can be done!

-(uint64_t)getFreeDiskspace {
    uint64_t totalSpace = 0;
    uint64_t totalFreeSpace = 0;
    NSError *error = nil;  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];  

    if (dictionary) {  
        NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];  
        NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
        totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
        totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
        NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
    } else {  
        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);  
    }  

    return totalFreeSpace;
}

But keep in mind, iOS cleanup by itself iOS cache folders, when system reach certain point of free space. So, it's very hard to manually make it full.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM