4

I've been reading a lot about tracking memory usage in Instrument's but found little in combination with Monotouch.

There seem to be to three oposing claims here:

  1. Use the Allocations utility of Instruments. The number of "live bytes" is the amount of physical memory used by the application.
  2. Use the Memory Monitor plugin. From the list of processes, pick your app and check the "Real memory" column. That's the amount of RAM currently in use.
  3. Use VM Tracker and make automatic snapshots. The "Dirty Size" if what you're after.

From what I've noticed:

  • "Real Memory" drops as soon as GC is triggered
  • Even if my "Live Bytes" remain around 30MB I will eventually catch memory warnings
  • With constant "Live Bytes", "Real Memory" can increase significantly and easily grow to 200MB or more.
  • While using QLPreviewController and viewing an insanly big Word document (1000 pages), scrolling through that document will grow real memory like crazy. If a memory warning is received, neither real memory, not live bytes drop at all. Eventually, the app will crash; Monotouch problem or Apple's problem?
  • Sometimes, real memory seems to grow and nothing can stop it. Then again, GC seems to clear big chunks of it. There is no real pattern in this.

So what is the correct answer? Is there exactly one?

EDIT: I attached two images. One showing memory usage in a stage in the middle of my app's life and the seconds one from way later. Both images reflect memory usage at the same point in the UI where nothing but two controllers are on screen. Maybe somebody can still comment what can be read from those number, especially the magic "Memory Tag 70".

enter image description here enter image description here

1 Answer 1

3

Instruments is somewhat of a black box, but here is how I think it is:

There seem to be to three opposing claims here:
1. Use the *Allocation*s utility of Instruments. The number of "live bytes" is the amount of physical memory used by the application.

I don't know exactly what "Live Bytes" is, but it's not the amount of physical memory used by the application. I think it is the amount of physical memory used by all ObjectiveC objects (if this theory is correct "Live Bytes" does not contain any memory used by managed code, nor any memory used indirectly by ObjectiveC objects (such as image data), which seems to be true). "Live Bytes" is definitively useful if you want to track down leaked objects, but it's not (necessarily) a good indicator on how much memory is actually in use.

2 . Use the Memory Monitor plugin. From the list of processes, pick your app and check the "Real memory" column. That's the amount of RAM currently in use.

This is a bit closer: "Real Mem" is the amount of physical memory the app is using which isn't shared with other apps. The total amount of physical memory the app is using is "Virtual Mem", but big chunks of "Virtual Mem" is shared between apps (i.e. a shared library will of course use memory when it's loaded in memory, but since it's immutable it will only be loaded once for all processes. It will however be added to each process's "Virtual Mem", so if you add up the "Virtual Mem" used by all processes you will go way beyond the actual physical memory your device has).

3 . Use VM Tracker and make automatic snapshots. The "Dirty Size" if what you're after.

Correct. "Dirty Size" is what you're after - this is however closely related to "Real Mem", it's just "Real Mem" split into categories so you can easily see what's using the memory.

For the typical case of using a lot of memory due to leaking images, the process goes like this:
1. Verify with the Memory Monitor that your app really has a memory problem.
2. See in VM Tracker / "Dirty Size" that a lot of memory is used by image data (that's the magic "Memory Tag 70").
3. Use Allocations to find out where CGImages are created, see the corresponding stack trace and track down why those images aren't freed.

Each app is different though, so it's not possible to come up with a short recipe which works for all cases.

  • "Real Memory" drops as soon as GC is triggered
  • Even if my "Live Bytes" remain around 30MB I will eventually catch memory warnings
  • With constant "Live Bytes", "Real Memory" can increase significantly and easily grow to 200MB or more.

All these are explained above.

  • While using QLPreviewController and viewing an insanly big Word document (1000 pages), scrolling through that document will grow real memory like crazy. If a memory warning is received, neither real memory, not live bytes drop at all. Eventually, the app will crash; Monotouch problem or Apple's problem?

It could be your problem too :) It's impossible to tell without actually knowing where the memory goes.

  • Sometimes, real memory seems to grow and nothing can stop it. Then again, GC seems to clear big chunks of it. There is no real pattern in this.

You mean you're watching real memory grow while your app is doing absolutely nothing? If you're actually doing something in your app this is completely normal.

3
  • About your comments to QLPreviewController and Real memory growth: there is nothing I'm aware of to do against QLPreviewController's memory usage. Just create a test app and give it a huge document and scroll and you'll see real memory grow. About the memory growth: I meant, sometimes Apps get killed with real memory at 60MB, sometimes with real memory at 200MB. And my feeling is that GC sometimes fails to give unuzsed memory back, but I cannot proof it (yet :-)).
    – Krumelur
    May 2, 2012 at 18:51
  • I verified and I can see that 50% of my dirty memory is coming from "Memory Tag 70" - what is this?
    – Krumelur
    May 2, 2012 at 19:09
  • @Krumelur: Updated answer to include info about memory tag 70 (it's image data) May 2, 2012 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.