First, get the device memory size method
//return storage memory consumption ratio-(NSString *) getfreediskspacerate{floatTotalspace; floatTotalfreespace=0. F; 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 floatvalue]/1024.0f/1024.0f/1024.0f; Totalfreespace= [Freefilesystemsizeinbytes floatvalue]/1024.0f/1024.0f/1024.0f; //totalstring, freestring is to define two global variables to display the size of the data on the progress bar withTotalstring =[self getfilesizestring:[filesystemsizeinbytes floatvalue]]; Freestring=[self getfilesizestring:[freefilesystemsizeinbytes floatvalue]]; NSLog (@"Print totalstring:%@,freestring:%@", totalstring,freestring); NSLog (@"memory capacity of%.2f GB with%.2f GB free Memory available.", Totalspace, totalfreespace); } Else{NSLog (@"Error Obtaining System Memory Info:domain =%@, Code =%@", [error domain], [error code]); } nsstring*FREESTR = [NSString stringWithFormat:@"%.3f", (Totalspace-totalfreespace)/totalspace];//progress bar ratio returnfreestr;}
Add: Another way to get memory size
First import # include <sys/param.h> and # include <sys/mount.h>
Method:
Nsfilemanager *FM =[Nsfilemanager Defaultmanager]; Nsdictionary*fat =[FM attributesoffilesystemforpath:nshomedirectory () Error:nil]; NSLog (@"Capacity:%lldg usable capacity:%lldg", [[Fat objectforkey:nsfilesystemsize]longlongvalue]/1000000000, [[Fat objectforkey:nsfilesystemfreesize]longlongvalue]/1000000000); structStatfs buf; Long LongFreeSpace =-1; if(Statfs ("/ var", &buf) >=0) {FreeSpace= (Long Long) (Buf.f_bsize *buf.f_bfree); } NSLog (@"%@", [NSString stringWithFormat:@"the remaining storage space for the phone is:%qi GB", freespace/1024x768/1024x768/1024x768]);
second, the method of obtaining the memory size value (g, M, KB units according to the actual situation)
-(NSString *) Getfilesizestring: (cgfloat) size{if(size>1024x768*1024x768*1024x768){ return[NSString stringWithFormat:@"%.1FG", size/1024x768/1024x768/1024x768];//greater than 1G, the string converted to G-Unit } Else if(size<1024x768*1024x768*1024x768&&size>=1024x768*1024x768)//greater than 1M, the string converted into M-units { return[NSString stringWithFormat:@"%.1FM", size/1024x768/1024x768]; } Else if(size>=1024x768&&size<1024x768*1024x768)//less than 1M, but more than 1KB, convert to KB units { return[NSString stringWithFormat:@"%.1FK", size/1024x768]; } Else//The rest is less than 1K, then converted into B units { return[NSString stringWithFormat:@"%.1FB", size]; }}
third, the virtual machine (the size of the space shown here is relatively large, indicating that the actual computer memory situation, the real machine on the normal display)
Iv. Other
reference URL : http://www.bubuko.com/infodetail-922638.html
IOS Get memory size usage (progress bar display)