33

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 some points where a single thread will perform a complex task and sometimes that task uses up enough memory that it is just terminated by the OS (my app can download pictures from the internet, and I scale them down to a small size ... if the user downloads a very large image, my app runs out of memory and just goes 'poof').

Having the App spontaneously terminate is obviously a poor user experience.

Is there any way that I can find out when I am about to run out of memory and stop the task instead?

I suppose I could put the task on a separate thread, and maybe the system would send the main thread a low memory warning, but that seems pretty complicated and not even guaranteed to work.

Thanks! Ron

Ron
  • 1,305
  • 2
  • 14
  • 22

7 Answers7

93

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);
    }
}
progrmr
  • 75,956
  • 16
  • 112
  • 147
  • The "freeMemory()" routine is exactly what I was looking for! Is there a reason why I shouldn't use this in product mode? I would like to know about how much memory is left, and then decide whether or not I can perform an operation that might make my app run out of memory. – Ron Nov 03 '11 at 16:32
  • As a follow up ... I used this on my original iPad, and I tried running a loop that just kept allocating small bits of memory. Interestingly, "free + used" kept changing (I guess not too surprising). My app started with 29MB free and 14MB used. As time went on, iOS would purge other apps from memory to make room for my app. When my app finally died, it had used up about 116MB. This is only about half of the memory on the device, but I guess the OS needs its share. After a fresh reboot, my App was able to get a bit more memory (125MB). – Ron Nov 03 '11 at 18:26
  • Looking at how memory works in iOS, I guess it is hard to tell how much memory might be available to my App, even with the routines you provided. Even when my App first was down to 2MB of free memory remaining, iOS was still able to free up another 80MB to keep my App going. I guess I might have to resort to checking the device, and then restricting operations based on how much memory those devices have available (the iPad2 can handle much larger images than a 2G iTouch for example). – Ron Nov 03 '11 at 18:28
  • 1
    I recommended only using it for testing because while it is a useful indicator when you are testing your app all by itself, in the real world it is more complex with other apps running as you noticed. – progrmr Nov 03 '11 at 20:04
  • @progrmr can you please comment on relationship of these values to what is shown in instruments? Is the usedMemory the same as what we see as resident size in VM tracker? – x89a10 Mar 27 '13 at 18:05
  • 1
    What exactly does `usedMemory` return? Currently I'm getting 100 megabytes (`usedMemory()/1024.0/1024.0` is `100.70`), yet I'm running the profiler and I have `11.25 MB` of "Live Bytes". The two numbers seem unrelated. EDIT: Also just checked out the `VM tracker` which is currently showing `129.41 MB` of resident size, whilst `usedMemory()` is returning `103.66` .. – Claudiu Oct 09 '13 at 15:40
  • 4
    size should be TASK_BASIC_INFO_COUNT instead of sizeof(info) - this mistake copy-pasted to many places with same code – Maxim Kholyavkin Dec 02 '13 at 01:14
  • How can we get the memory usage of other application ? – Amit Khandelwal Jul 28 '15 at 11:05
  • 1
    I just wonder why the memory get by that code is 2-3 times bigger than XCode shows, any ideas? – Julian Oct 06 '15 at 07:42
  • 1
    @progrmr why you divide by 1000 instead of 1024? – Julian Oct 06 '15 at 07:50
  • 1
    @Julian that is to match the way Apple displays memory and disk size on OS X and iOS. – progrmr Oct 06 '15 at 15:38
  • @progrmr "that is to match the way Apple displays" - could you provide some link ? – olha Feb 06 '19 at 17:13
3

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

Nimbus' Overview Tool

featherless
  • 2,118
  • 19
  • 19
3

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.

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • Thanks for the response! I actually do monitor this routine, but I found that this routine is only called when the memory gradually increases. If I have a single operation that allocates a huge amount of memory, my app just gets terminated and no warning message is ever sent. – Ron Nov 03 '11 at 16:34
3

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..

  • Now to see the real time memory usage of your app you can use allocator tool in instrument
  • To identify the memory leaks you can use leak tool in instrument..

Also in WWDC 2010 there is a video of how to analyze memory using Instrument..

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • 1
    Thank you very much for the pointers! The xcode instruments tools are very useful! However, I don't think I explained my problem as well as I should have. In my case, my App is using system calls to manipulate a photo. I don't think there is any way I can reduce the memory used by this operation. When a very large photo is selected by the user, my app will often just terminate due to excessive memory usage. I was looking for a way in code to see how much memory is left so I can decide whether or not there is enough to perform an expensive operation. – Ron Nov 03 '11 at 16:39
2

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
Robert Taylor
  • 459
  • 1
  • 4
  • 9
2

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.

Marc Brannan
  • 354
  • 2
  • 7
  • Thanks for the pointer to instruments! They are very useful! I think the subject of my message was a little misleading, however. I am actually looking for a way in code to determine how much memory is available so I can decide whether or not I can perform expensive operations. – Ron Nov 03 '11 at 16:40
1

Sounds like you could use a well crafted library for the task of fetching web images. Nimbus has got a Network Image class which does that efficiently.

fabb
  • 11,660
  • 13
  • 67
  • 111