iOS code get memory size

Source: Internet
Author: User

#include <mach/mach.h>

// Storage memory

-(float) getfreediskspace{

float totalspace;

float totalfreespace=0.f;

nserror *error = nil;

nsarray *paths =nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, YES);

nsdictionary *dictionary = [[nsfilemanagerdefaultmanager] Attributesoffilesystemforpath: [Pathslastobject] error: &error];

if (dictionary) {

nsnumber *filesystemsizeinbytes = [Dictionary objectforkey: nsfilesystemsize];

nsnumber *freefilesystemsizeinbytes = [Dictionaryobjectforkey:nsfilesystemfreesize];

Totalspace = [filesystemsizeinbytesfloatvalue]/1024.0f/1024.0f/1024.0f;

Totalfreespace = [freefilesystemsizeinbytesfloatvalue]/1024.0f/1024.0f/1024.0f ;

NSLog(@ "Memory capacity of%f GB with%f GB free Memory available." , Totalspace, totalfreespace);

}Else {

NSLog(@ "error obtaining System Memory Info:domain =%@, Code =%@", [Error domain ], [Error code ]);

}

return Totalfreespace;

}

// available running memory

-(double) getavailablebytes

{

vm_statistics_data_t vmstats;

mach_msg_type_number_t infocount =host_vm_info_count;

kern_return_t kernreturn = host_statistics(mach_host_self(), host_vm_info, ( host_info_t) &vmstats, &infocount);

if (Kernreturn! = kern_success)

{

return Nsnotfound;

}

return (vm_page_size * vmstats. Free_count) + (vmstats. Inactive_count * vm_page_size);

}


-(double) getavailablekilobytes

{

return [selfgetavailablebytes]/ 1024.0;

}


-(double) getavailablemegabytes

{

return [selfgetavailablekilobytes]/ 1024.0;

}

//-----------------------------------------------------

Various types of memory

Original URL http://www.cnblogs.com/bandy/archive/2012/08/15/2639742.html

BOOL memoryinfo (vm_statistics_data_t *vmstats) {
mach_msg_type_number_t infocount = Host_vm_info_count;
kern_return_t Kernreturn = Host_statistics (Mach_host_self (), Host_vm_info, (host_info_t) vmStats, &infoCount);

returnKernreturn = = kern_success;
}

voidLogmemoryinfo () {
vm_statistics_data_t Vmstats;

if(Memoryinfo (&vmstats)) {
NSLog (@"Free :%u\nactive:%u\ninactive:%u\nwire:%u\nzero fill:%u\nreactivations:%u\npageins:%u\npageouts:%u\nfaults:% U\ncow_faults:%u\nlookups:%u\nhits:%u",
Vmstats.free_count * Vm_page_size,
Vmstats.active_count * Vm_page_size,
Vmstats.inactive_count * Vm_page_size,
Vmstats.wire_count * Vm_page_size,
Vmstats.zero_fill_count * Vm_page_size,
Vmstats.reactivations * Vm_page_size,
Vmstats.pageins * Vm_page_size,
Vmstats.pageouts * Vm_page_size,
Vmstats.faults,
Vmstats.cow_faults,
Vmstats.lookups,
Vmstats.hits
);
}
}

call Memoryinfo () to get the memory information, its type isvm_statistics_data_t. This struct has many fields and shows how to get them in Logmemoryinfo (). Note that most of these fields are the number of pages, multiplied by vm_page_size to get the number of bytes.
by the way: Free is idle memory, active is used, but can be paged (in iOS, only the static on the disk can be paged, such as the memory map of the file, and dynamically allocated memory is not paged); inactive is inactive, This is the memory that is not released after the program exits, in order to speed up the start again, and when the memory is low, it will be recycled, so it can also be considered as free memory; a wire is already in use and cannot be paged.

In the end, you'll find that even if you add all of this to a much smaller amount of memory than the device, the rest of the memory will have to be taken up as a mystery. But on the simulator, these 4 add up basically is the physical memory of the Mac, the difference is less than 2MB.
and the total physical memory can be obtained with nsrealmemoryavailable (), this function does not need to provide parameters, the document is also documented, I do not write the demo code.

iOS code get memory size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.