6

I am working on a Browser application in which I use a UIWebView for opening web pages. I run the Instruments tool with Memory Monitor. I am totally confused by the terms which are used in Instruments and why they're important. Please explain some of my questions with proper reasons:

  1. Live Bytes is important for checking memory optimization or memory consumption? Why ?

  2. Why would I care about the Overall Bytes/ Real Memory, if it contains also released objects?

  3. When and why are these terms used (Live Bytes/ Overall Bytes/Real Memory)?

Thanks

Tom Sawyer
  • 596
  • 5
  • 17

1 Answers1

12

"Live Bytes" means "memory which has been allocated, but not yet deallocated." It's important because it's the most easily graspable measure of "how much memory your app is using."

"Overall Bytes" means "all memory which has ever been allocated including memory that has been deallocated." This is less useful, but gives you some idea of "heap churn." Churn leads to fragmentation, and heap fragmentation can be a problem (albeit a pretty obscure one these days.)

"Real Memory" is an attempt to distinguish how much physical RAM is in use (as opposed to how many bytes of address space are valid). This is different from "Live Bytes" because "Live Bytes" could include ranges of memory that correspond to memory-mapped files (or shared memory, or window backing stores, or whatever) that are not currently paged into physical RAM. Even if you don't use memory-mapped files or other exotic VM allocation methods, the system frameworks do, and you use them, so this distinction will always have some importance to every process.

EDIT: Since you're clearly concerned about memory use incurred by using UIWebView, let me see if I can shed some light on that:

There is a certain memory "price" to using UIWebView at all (i.e. global caches and the like). These include various global font caches, JavaScript JIT caches, and stuff like that. Most of these are going to behave like singletons: allocated the first time you use them (indirectly by using UIWebView) and never deallocated until the process ends. There are also some variable size global caches (like those that cache web responses; CFURL typically manages these) but those are expected to be managed by the system. The collective "weight" of these things with respect to UIWebView is, as you've seen, non-trivial.

I don't have any knowledge of UIKit or WebKit internals, but I would expect that if you had a discussion with someone who did, their response to the question of "Why is my use of UIWebView causing so much memory use?" would be two pronged: The first prong would be "this is the price of admission for using UIWebView -- it's basically like running a whole web browser in your process." The second prong would be "system framework caches are automatically managed by the system" by which they would mean that, for instance, the CFURL caches (which is one of the things that using UIWebView causes to be created) are managed by the system, so if a memory warning came in, the system frameworks would be responsible for evicting things from those caches to reduce the memory consumed by them; you have no control over those, and you just have to trust that the system frameworks will do what needs to be done. (That doesn't help you in the case where whatever the system cache managers do isn't aggressive enough for you, but you're not going to get any more control over them, so you need to attack the issue from another angle, either way.) If you're wondering why the memory use doesn't go down once you deallocate your UIWebView, this is your answer. There's a bunch of stuff it's doing behind the scenes, that you can't control.

The expectation that allocating, using, and then deallocating a UIWebView is a net-zero operation ignores some non-trivial, inherent and unavoidable side-effects. The existence of such side-effects is not (in and of itself) indicative of a bug in UIWebView. There are side effects like this all over the place. If you were to create a trivial application that did nothing but launch and then terminate after one spin of the run loop, and you set a breakpoint on exit(), and looked at the memory that had been allocated and never freed, there would be thousands of allocations. This is a very common pattern used throughout the system frameworks and in virtually every application.

What does this mean for you? It means that you effectively have two choices: Use UIWebView and pay the "price of admission" in memory consumption, or don't use UIWebView.

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • Thank you, You answer is very good, I understand , I need one clarification on which one have to keep eye on every time to managed the Memory consumption and Memory optimization in these three and why? can you please help me out. – Tom Sawyer Aug 29 '13 at 12:34
  • 1
    In the general case, Live Bytes is the thing you care about. – ipmcc Aug 29 '13 at 12:34
  • Live bytes and i read [here] (http://stackoverflow.com/questions/9660763/whats-the-right-statistic-for-ios-memory-footprint-live-bytes-real-memory-ot), Live bytes is useful to check when you allocate and release memory in your code. Real memory is good indicator for memory optimization efficiency. And it's overhead causes 'low memory' warnings. That is the reason, I m confused here Sir, Please brief me? – Tom Sawyer Aug 29 '13 at 12:37
  • Meaning is that which one is responsible for that, and if my live bytes is not more that 4-5 MB why Real Memory increased 60-70 MB? and how to optimized real memory ? – Tom Sawyer Aug 29 '13 at 12:42
  • Yes, "Real Memory" is sometimes more important, since it can include memory that you didn't explicitly allocate, but which was allocated on your behalf by other things (including other processes) but this is sort of a question of "first order problems" vs "second order problems." The first place to start is with the stuff that *you* allocate. Once you've established that your memory use is contained and reasonable, you can then start to look at "second order" uses of memory. So, my advice stands, start by looking at Live Bytes. Worry about the other stuff only if you have to. – ipmcc Aug 29 '13 at 12:42
  • 1
    I have added some long-form explanation of UIWebView's memory implications to my answer. Hopefully they'll provide some insight, and let you move on with more interesting issues. :) (I don't actually type that fast, but I answered a very similar question for someone else on a different forum, and cribbed heavily from that...) – ipmcc Aug 29 '13 at 12:52
  • Great effort, very useful information you provided.. Thanks you IPMCC sir.. Thanks – Tom Sawyer Aug 29 '13 at 13:15
  • Heap fragmentation can be a pretty big deal occasionally. I once reduced Adium's memory usage on a stress test by 50MB... by *caching more*. More live bytes, but way less transient ones, and it ended up mattering that time. – Catfish_Man Aug 30 '13 at 07:07
  • Sure; I'm not saying heap fragmentation isn't a problem. But I suspect it's far less of a problem in the iOS world of on-demand app killing and resuming and persistent app state. Put differently, I expect most (non-pathological) iOS apps don't stay alive long enough for heap fragmentation to become a substantial issue (*and* if heap frag did force an app's high water mark high enough, the app would get killed for memory pressure -- oh the irony!) – ipmcc Aug 30 '13 at 11:32