Is it possible to monitor the amount of memory your app is consuming?
-
All the answers point to tools you can use in XCode when a device is tethered. **I would love to know, if an app can determine how much memory it is currently using, when it is actually running in the wild (on a consumer's phone).** If anyone knows this - awesome. – Fattie Jan 02 '11 at 19:52
-
2@Joe - Ben shows a way to do this using Mach functions here: http://stackoverflow.com/questions/2798638/available-memory-for-iphone-os-app/2798771#2798771 , which is identical to what Andrew has in his answer. – Brad Larson Jan 04 '11 at 22:28
-
possible duplicate of [Programmatically retrieve memory usage on iPhone](http://stackoverflow.com/questions/787160/programmatically-retrieve-memory-usage-on-iphone) – CRABOLO Apr 25 '15 at 03:37
6 Answers
Actually, it's probably more important you know how much memory is free, rather than how much your app is using. Here's some code to do that:
#import <mach/mach.h>
#import <mach/mach_host.h>
+(natural_t) get_free_memory {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
NSLog(@"Failed to fetch vm statistics");
return 0;
}
/* Stats in bytes */
natural_t mem_free = vm_stat.free_count * pagesize;
return mem_free;
}

- 10,694
- 5
- 60
- 84

- 10,175
- 10
- 58
- 75
-
1Thanks this was very helpful. We put it in a callback loop to compare memory footprints of two different approaches. We felt like the results from this were more consistent and made more sense then from instruments. – Kibitz503 Jul 24 '12 at 20:29
-
3Can I ask why it would be more useful to know how much memory is free as opposed to how much is being used by the current app? How much is free only affects how much lag spiking is in the near future should allocation be performed, while total usage would inform whether **app termination** is imminent. I cannot imagine a single situation where the former is more useful to know than the latter. I direct readers [here](http://stackoverflow.com/a/7990532/340947) for a way to find out current app usage. – Steven Lu Sep 04 '13 at 03:06
-
3When someone asks for A, don't tell him/her A is not as important as B. – Jason Fuerstenberg Feb 19 '16 at 21:06
-
I got around 110 MB, What that means? Is that total memory for my app? – Mathi Arasan Jul 10 '17 at 14:32
-
according to your code my Today Extension has free space of 857Mb while max 16Mb is allowed. Downvoted – Vyachaslav Gerchicov Feb 02 '18 at 11:48
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. You can use this with both, the iOS Simulator and an actual device. You should prefer to analyze the app while running on an iOS device to get optimal results.
Instruments can do a lot more to help you optimize your apps, so you should give the Instruments User Guide a closer look.

- 1,314
- 11
- 18
-
This is now Product -> Analyze -> Allocations. Stop moving stuff around, Apple, it's obnoxious. – Glenn Maynard Aug 11 '12 at 18:47
-
5
You can use Instruments. It is provided with iOS SDK.
It is more accurate with a device than the simulator...
Launch it, choose a type of monitoring (Allocation, Leaks, Activity Monitor), choose process and target to monitor and then click on the record button.
Clicking on this button, the app opens by itself.
When you've finished, click on the stop button to stop monitoring.
You can find more informations about this program here: About Instruments

- 4,637
- 39
- 52
- 71

- 3,992
- 5
- 39
- 73
you can check with this url: https://github.com/andrealufino/ALSystemUtilities/blob/develop/ALSystemUtilities/ALSystemUtilities/ALDisk/ALDisk.m this is working fine.

- 151
- 5
-
This is fine. But instead of posting simply Link , please try posting some explanations. – Abdul Yasin Aug 05 '16 at 12:12
You can check your running memory here. Wont give details of what is consuming but a good total amount of memory.

- 11,305
- 5
- 36
- 34
If you have an apple developer account check out the current WWDC about instruments and optimizing memory on ios. It is really worth seeing if you which to quickly understand how instruments are working.

- 25,734
- 24
- 99
- 122