Memory Management/Instruments on the iPhone
I'm developing an RSS reader type iPhone application and it is nearing completion, however I upgraded to Xcode 4 with iOS 4.3 yesterday and I have run into some serious memory issues. My App now frequently gets memory warnings, I'm handling didReceiveMemoryWarning, and releasing all my cached images etc, but this does not provide a good user experience. I've also been using Instruments in an attempt to diagnose the problem, but this has been bringing up more questions then answers.
• Does the SDK 4.3 use more memory ? I wasn't receiving memory warnings before I upgraded. Not sure if this is relevant or not, but I'm doing most of my testing on an iPhone 3G with iOS 4.2 on it (4.3 is not supported on a 3G).
• Why does the allocations instrument tool live bytes column not match the real memory column in the activity monitor tool ? The activity monitor tool always reports more memory usage.
• According to leaks my app does not leak memory, in the simulator or on an actual iPhone, but when I look at allocations, it seems that as I transition between views and then pop views, the live bytes column does not return to its previous value, which is consistent with leaking memory ? (Yes subviews are released after they are popped)
• My app frequently reports a memory warning on startup, but only when I'm profiling the application with leaks. Does leaks cause extra memory overhead ?
• Perhaps this is a difficult question to answer, but what is a reasonable memory footprint fo开发者_StackOverflow社区r an iPhone app ? When I'm running leaks, my app will almost certainly be killed due to low memory a few minutes into being run, but allocations in the leaks tool reports I'm using less then 2 MB when the app is terminated.
• Why does the activity monitor instrument have a column for virtual memory ? Everything I've read states that the iPhone does not utilize virtual memory ?
The 4.3 SDK probably does use more memory, but if that's a question you're asking then you're probably on the wrong track, because as a developer that should not factor into how stable your app is.
I would not worry about the discrepancy between what Allocations reports and what Activity Monitor reports. It is also not unusual for the memory footprint to grow, as views are pushed and popped, and not shrink. Another answer on how free(3)
works explains this phenomenon in better detail.
A reasonable memory footprint for an iOS app is the lowest amount your app needs in order to function.
iOS does indeed use virtual memory. From Apple's Memory Usage Performance Guidelines / About the Virtual Memory System:
Both Mac OS X and iOS include a fully-integrated virtual memory system that you cannot turn off; it is always on. Both system also provide up to 4 gigabytes of addressable space per 32-bit process.
...
Although Mac OS X supports a backing store, iOS does not. In iPhone applications, read-only data that is already on the disk (such as code pages) is simply removed from memory and reloaded from disk as needed. Writable data is never removed from memory by the operating system. Instead, if the amount of free memory drops below a certain threshold, the system asks the running applications to free up memory voluntarily to make room for new data. Applications that fail to free up enough memory are terminated.
I recommend you read through the whole document, especially Finding Memory Leaks. You can also use the Build-and-Analyze feature, which uses the built-in Clang to statically detect several kinds of bugs, including memory leaks.
精彩评论