16

While running my app, I sometimes get Memory Warning. It doesn't always happen in same place in the code, but I suspect it has something to do with memory allocated from outside of my app; i.e. if there are many applications running on the iPhone in the background I will get the memory warning earlier, and if there are no applications running in the background I will not get the warning at all.

  1. Is there a method I can use to check how much memory my application is using at a certain point?
  2. Is there a method I can use to check how much memory the machine (iPhone/iPad) is using at a certain point? maybe even to check how much memory is still available to use?
  3. In general, maybe somebody knows where I can find data about the memory limitations for the different iOS based machines?

Thanks, Ohad

Ohad Regev
  • 5,641
  • 12
  • 60
  • 84
  • I suppose you have checked for memory leaks in your own app? That is usually the first thing to do when you get memory warnings. – Manuel Jun 22 '11 at 11:05
  • Hi dragon112, yes I did check for leaks using the Instruments Tool and already solved all of them (all of the NSStrings that surprisingly leak...). I still get the memory warnings and have no idea how to "catch" them while they are happening. I currently don't know which object is causing them. – Ohad Regev Jun 22 '11 at 12:30
  • Currently when I run Instruments Tool with "Leaks" scheme I come out clean, but on the application LOG I can see I get Memory Warning 1 and 2. – Ohad Regev Jun 22 '11 at 12:32
  • That's odd, does your app use a large amount of memory? And do these warnings occur when you just started with the app or only when you have been playing around for a while with the app? – Manuel Jun 22 '11 at 12:33
  • 2
    I don't know how to check exactly how much memory my app is using (if you have a tip here I would appreciate it). I do have several Images allocations "living" in the same time and being released at the end of a user set of actions (I can release some of them as we go along, but it causes other memory allocation issues; if I'm not sure this is what causing the memory warning, i prefer not to go there). Regarding your question about when it happens - If there are other applications running, it happens in the first set of user actions, and if there are no other apps it simply doesn't happen!! – Ohad Regev Jun 22 '11 at 12:54
  • First of all i beleve when you are in the memory leak program there are 2 bars, one is to see allocated memory one for leaks. in the first you can find how much memory is allocated if im not mistaken. That it hapens only when other apps are running is weird.. i have no awnser for that, sorry. – Manuel Jun 22 '11 at 12:57

4 Answers4

15

Using Instruments you can check how much memory your app is using. In Xcode4, use 'Profile' build, choose Leaks, then click the Library button in the toolbar and add the Memory Monitor instrument.

It will show an overview of every app that is running and how much memory each is using.

Pieter
  • 17,435
  • 8
  • 50
  • 89
  • 3
    +1 for the Memory Monitor instrument. Had no idea that existed. Note that for iOS7 devices in Xcode 5 you can see the real-time memory usage on Xcode's Debug Navigator, this doesn't work for iOS6 devices hence why above is still useful. – Ben Clayton Oct 03 '13 at 14:35
  • For xcode 8.2 see [this](https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/MonitoringMemoryUsage.html) – LC 웃 Feb 02 '17 at 09:13
2

You can check the memory usage, using vm_statistics_data_t object. Kindly find the details and implementation here:

  1. http://gamesfromwithin.com/whered-that-memory-go
  2. http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
Say2Manuj
  • 709
  • 10
  • 20
2

If you don't want to use Instruments, there is actually a utility class that Giulio Petek wrote that gets the current memory usage.

Try it out here: http://forrst.com/posts/Get_current_Memory_usage-hzw

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
-4

You shouldn't worry about checking how much memory is available. The OS manages the memory and will issue warnings to the top memory consumers when memory is running low. What you need to do is make sure you're properly handling didReceiveMemoryWarning messages and getting rid of any data that can be reloaded on demand later. Also, if your app is experiencing performance problems, you should use Instruments to check your apps usage in common situations and be sure you're using memory efficiently and only loading large objects that MUST be in memory at that time.

Paul Tiarks
  • 1,881
  • 1
  • 13
  • 10