How to get available memory in IOS ? [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get information about free memory and running processes in an App Store approved app? (Yes, there is one!)
How to get available memory in IOS ? I don't know..
I googled, then I know how can i get the physical memory, user memory and used mem, free mem in VM.
But. the memory size is different with which is Settings -> General -> About -> Avaliable.
I want to know "Avaliable" in Settings.
Have a nice day .

found this on here a few months back cant remember who originally posted. Is this what you are looking for?
- (uint64_t)freeDiskspace
{
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
__autoreleasing 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;
}

Related

Why does NSFileManager disagree with statfs about disk space remaining?

I've tried two implementations on iOS 12 to get the remaining free disk space.
"The Apple Way"
uint64_t get_disk_bytes_free()
{
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 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];
}
else
{
LOG_APP("Error Obtaining System Memory Info: Domain = %s, Code = %ld", [[error domain] UTF8String], (long)[error code]);
}
return totalFreeSpace;
}
"The Unix Way" - path is still the Library/Caches path.
struct statfs fs;
if(statfs(path.c_str(),&fs)==0)
{
if(out_total_space) *out_total_space = (xu64) fs.f_bsize * (xu64) fs.f_blocks;
if(out_free_space) *out_free_space = (xu64) fs.f_bsize * (xu64) fs.f_bfree ;
return err_code_ok;
}
The Apple Way returns 4GB free. The Unix Way returns about 10GB free. They both equally report 16GB total (which is correct) but this device has hardly NOTHING on it. Just a couple of apps.
If I go to iPhone Storage in the Settings to see, it shows 11.8GB of 16GB used....almost all of it is used by "System" at the very bottom (8.65GB).
This System usage seems to disappear if i reboot the device but it comes back eventually.
Can someone tell me if there's any good reason for the discrepancy between these two methods? Which one is more reliable/accurate?

Can we check programmatically if iOS device has low memory situation?

I have a situation where lots of transactions have to uploaded to the server. I have made this job asynchronously using NSManagedObjectContext performBlock: method that performs block on the NSManagedObjectContext queue. So in the async-block how can I observe the low memory warning! Instead of observing I would like to determine if the device has enough free memory then only upload data. Otherwise if it has low-memory situation then wait till low-memory happens and then resume the upload job.
You can check free disk space of your iOS device before upload data to avoid low-memory situation.
+(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 = %ld", [error domain], (long)[error code]);
}
return totalFreeSpace;
}

How to check sufficient space on iPhone device before downloading audio files from server

i am able to download audio files successfully.. i am able to get the device free space….but my requirement is before downloading file i want to check is there enough space on iPhone if yes then download and if not then user can see some alert message like “your device does not have enough memory space.”
plz guide me any body.. Thanks in advance.
- (uint64_t)freeDiskspace
{
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
__autoreleasing 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 = %ld", [error domain], (long)[error code]);
}
return totalFreeSpace;
}
You wouldn't download a 12.8MB audio file if you have 12.9MB free space. That's asking for trouble. Check for 100MB free space. Then check if an iPhone misbehaves or gives the user trouble with only 100MB free space, and increase the number if it does.

AVFoundation Recording Not Enough Disk Space Error Code 11807

I am writing a small video that is only 100kb in size. My phone has about 20mb left on disk, but when I try create a recording using AVFoundation and AVCaptureMovieFileOutput, I get the error:
2014-06-15 10:40:19.511 wave[551:690b] ERROR:
Error Domain=AVFoundationErrorDomain
Code=-11807 "Operation Stopped" UserInfo=0x157cde20
{NSLocalizedRecoverySuggestion=There
is not enough available space to continue the file writing.
Make room by deleting existing videos or photos
I am setting the
self.movieFileOutput.minFreeDiskSpaceLimit = //space available in bytes
thinking it would override this default behavior, but it's not. Any idea how to continue allow for recording given the disk space parameters?
Update - here is the code I'm using to get free space:
-(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 = %#", error);
}
return totalFreeSpace;
}

Get System Information such as OS Version, battery, storage, memory and so on [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on an app which needs to display some device information, Could some one help me to verify if that's legal to pull this information and maybe some code snippets would be very helpful.
Thanks in advance !
Device information:
Remain Battery Status
Is phone charging
Device OS version
Current App version
Available Memory
Available Storage
I think all those information are legal to pull. and here are some code for each of them
Remain battery:
UIDevice *device = [UIDevice currentDevice];
[device setBatteryMonitoringEnabled:YES];
float remainBatteryLife = [myDevice batteryLevel];
Phone charging status
//same device object as the previous one
UIDevice *device = [UIDevice currentDevice];
[device setBatteryMonitoringEnabled:YES];
int i=[myDevice batteryState];
switch (i)
{
case UIDeviceBatteryStateUnplugged:
//Unplugged
break;
case UIDeviceBatteryStateCharging:
//Charging
break;
case UIDeviceBatteryStateFull:
//full
break;
default:
break;
}
OS version
NSString *OSVersion = [[UIDevice currentDevice] systemVersion];
App version
NSString *majorVersion = [infoDictionary objectForKey:#"CFBundleShortVersionString"];
NSString *minorVersion = [infoDictionary objectForKey:#"CFBundleVersion"];
Available Memory (For this question, i used some C codes instead of Objective-C, but that returns exactly like itunes would tell you, inspired by https://sites.google.com/site/iphonedevnote/Home/get-available-memory)
//import some C libraries first
#import <mach/mach.h>
#import <mach/mach_host.h>
//then put these code in whichever method it needs to be
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
NSLog(#"Failed to fetch vm statistics");
/* Stats in bytes */
natural_t mem_used = (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count) * pagesize;
natural_t mem_free = vm_stat.free_count * pagesize;
natural_t mem_total = mem_used + mem_free;
natural_t memoryFactor = 1024;
NSLog(#"used: %u MB free: %u MB total: %u MB", (mem_used / memoryFactor) / memoryFactor, (mem_free / memoryFactor) /memoryFactor, (mem_total /memoryFactor) /memoryFactor);
Disk space (code source From Code Warrior on https://stackoverflow.com/a/8036586/3276557)
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]);
}
You are looking for the UIDevice class. Some code sni
UIDevice *currentDevice = [UIDevice currentDevice];
NSString * batteryLevel = [currentDevice batteryLevel];
NSString *systemVersion = [currentDevice systemVersion];

Resources