Is there any way to find out how much memory is available in iOS? I know that the system will pass low memory warnings when available memory gets low. However, my App has so
Actually each view controller has - (void)didReceiveMemoryWarning
functions.
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
As suggested by the comments, you can release unused data under the comment. On the other hand, comment out [super didReceiveMemoryWarning];
to suppress memory warnings & auto release objects.
I recommend checking out Nimbus' Overview tool for watching device statistics in real time. Out of the box it includes pages for viewing available memory, disk space and logs, as well as modifying log levels. It's also easy to add custom pages that show any information you want.
http://latest.docs.nimbuskit.info/NimbusOverview.html
I love free code. Thank you progrmr, very useful. Time for me to start sharing back. I object-orientified it for my own use case.
#import "mach/mach.h"
#import "memusage.h"
@implementation memusage
static long prevMemUsage = 0;
static long curMemUsage = 0;
static long memUsageDiff = 0;
static long curFreeMem = 0;
-(vm_size_t) freeMemory {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
vm_size_t pagesize;
vm_statistics_data_t vm_stat;
host_page_size(host_port, &pagesize);
(void) host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
return vm_stat.free_count * pagesize;
}
-(vm_size_t) usedMemory {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
return (kerr == KERN_SUCCESS) ? info.resident_size : 0; // size in bytes
}
-(void) captureMemUsage {
prevMemUsage = curMemUsage;
curMemUsage = [self usedMemory];
memUsageDiff = curMemUsage - prevMemUsage;
curFreeMem = [self freeMemory];
}
-(NSString*) captureMemUsageGetString{
return [self captureMemUsageGetString: @"Memory used %7.1f (%+5.0f), free %7.1f kb"];
}
-(NSString*) captureMemUsageGetString:(NSString*) formatstring {
[self captureMemUsage];
return [NSString stringWithFormat:formatstring,curMemUsage/1000.0f, memUsageDiff/1000.0f, curFreeMem/1000.0f];
}
@end
First the title of your question is how to watch memory usage in iOS..There is a tool called instrument comes with xcode, which you can use to track memory allocation, leaks, cpu usage and a host of other things..See apple's documentation on the subject..
Also in WWDC 2010 there is a video of how to analyze memory using Instrument..
While testing and debugging your app with XCode you can use this logMemUsage()
function to NSLog the used/free space and watch how things are going while you test your app. This function logs any change in usage > 100kb. It outputs to the debug log like this (on the simulator the free space is huge):
2011-11-02 21:55:58.928 hello[971:207] Memory used 21884.9 (+21885), free 1838366.8 kb
2011-11-02 21:55:59.936 hello[971:207] Memory used 28512.3 (+6627), free 1830809.6 kb
2011-11-02 21:56:01.936 hello[971:207] Memory used 28803.1 ( +291), free 1830129.6 kb
2011-11-02 21:56:02.936 hello[971:207] Memory used 29712.4 ( +909), free 1830142.0 kb
You decide where to call logMemUsage
in your app. I happen to have a function that is called by a timer every second and so I put it in there. I suggest using #ifdef
around these so this code is only included in Debug builds.
#import "mach/mach.h"
vm_size_t usedMemory(void) {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
return (kerr == KERN_SUCCESS) ? info.resident_size : 0; // size in bytes
}
vm_size_t freeMemory(void) {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
vm_size_t pagesize;
vm_statistics_data_t vm_stat;
host_page_size(host_port, &pagesize);
(void) host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
return vm_stat.free_count * pagesize;
}
void logMemUsage(void) {
// compute memory usage and log if different by >= 100k
static long prevMemUsage = 0;
long curMemUsage = usedMemory();
long memUsageDiff = curMemUsage - prevMemUsage;
if (memUsageDiff > 100000 || memUsageDiff < -100000) {
prevMemUsage = curMemUsage;
NSLog(@"Memory used %7.1f (%+5.0f), free %7.1f kb", curMemUsage/1000.0f, memUsageDiff/1000.0f, freeMemory()/1000.0f);
}
}
The suite of development tools that apple provides includes "Instruments". You can use this to monitor allocations and leaks. In Xcode if you long click on the Run button you will see an option called "Profile". This will open up instruments automatically and allow you to select a profile to monitor your application.