Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watching memory usage in iOS

Tags:

ios

iphone

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

like image 958
Ron Avatar asked Nov 03 '11 03:11

Ron


People also ask

How can I monitor my iPhone memory?

Go to Settings > General > [Device] Storage. You might see a list of recommendations for optimizing your device's storage, followed by a list of installed apps and the amount of storage each one uses. Tap an app's name for more information about its storage. Cached data and temporary data might not be counted as usage.

How can I see memory usage history?

Check Computer Memory Usage EasilyTo open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.

Can we check RAM usage of particular app on iPhone?

Yes. In Xcode, open your project and choose Run > Run with Performance Tool > Allocations. This will start an application called Instruments, which can be used to analyze your app. In that specific case it will record all object allocations which gives you a good overview of your memory footprint.

How does IOS monitor storage on your device?

iOS monitors the storage on your device by analyzing how much space each app uses. You can also check the storage on your device in Settings or in iTunes. If your device is low on storage, iOS automatically frees up space while installing an app, updating iOS, downloading music, recording videos, and more.

How do I check how much storage space my iOS device has?

Use iTunes to check the storage on your iOS device. Open iTunes on your computer. Connect your iOS device to your computer. Select your iOS device in iTunes. You'll see a bar that shows how much storage your iOS content uses, divided by content type.

How does IOS optimize storage?

How iOS optimizes storage. If your device is low on storage, iOS automatically frees up space while installing an app, updating iOS, downloading music, recording videos, and more. To make more storage available, iOS can remove some of your items, like streamed music and videos, files in iCloud Drive, and parts of apps that aren't needed.

How do I Check my Device's storage usage?

Go to Settings > General > [Device] Storage. You might see a list of recommendations for optimizing your device's storage, followed by a list of installed apps and the amount of storage each one uses. Tap an app's name for more information about its storage. Cached data and temporary data might not be counted as usage.


3 Answers

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);
    }
}
like image 194
progrmr Avatar answered Oct 11 '22 18:10

progrmr


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.

like image 38
Raptor Avatar answered Oct 11 '22 18:10

Raptor


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

like image 41
Krishnabhadra Avatar answered Oct 11 '22 20:10

Krishnabhadra