Simple memory question: UIKit

Hi


I‘ve been trying to wrap my head round memory management etc. and noticed that simply raising a UIAlert (with a simple text field, save and cancel buttons) bumps up my memory usage by about 12 to 14mb. The memory doesn’t appear to get released, although it doesn’t keep increasing either.


Is this normal? I know it’s not much, just curious, seems a lot for such a simple tool.


Cheers

Answered by QuinceyMorris in 305334022

Memory usage — how to measure it, I mean — is not a simple thing. UIAlert is implemented in a library, which is going to occupy space in your process's address space even if it doesn't occupy additional physical memory (because it's shared with other processes and already loaded). Using it may cause other libraries and their associated data to be loaded, so you may see a usage bump that you would have seen sometime or other. Using it may also cause data to be cached, which temporarily comes under your memory usage accounting, but can be freed if the memory is needed for other things. And so on.


So, yes, this kind of behavior is normal. You can (and should eventually) use Instruments to check that your app isn't inadvertently holding onto its own memory data structures longer than expected, and you should keep an eye on the usage to make sure that it doesn't keep growing. Other than that, you have no particular reason to think there's anything wrong.

Accepted Answer

Memory usage — how to measure it, I mean — is not a simple thing. UIAlert is implemented in a library, which is going to occupy space in your process's address space even if it doesn't occupy additional physical memory (because it's shared with other processes and already loaded). Using it may cause other libraries and their associated data to be loaded, so you may see a usage bump that you would have seen sometime or other. Using it may also cause data to be cached, which temporarily comes under your memory usage accounting, but can be freed if the memory is needed for other things. And so on.


So, yes, this kind of behavior is normal. You can (and should eventually) use Instruments to check that your app isn't inadvertently holding onto its own memory data structures longer than expected, and you should keep an eye on the usage to make sure that it doesn't keep growing. Other than that, you have no particular reason to think there's anything wrong.

This is where the memory graph debuggnig toolis helpful, you can see if your alert instance hangs around in memory beyond what you expect. With UIAlertController specifically, I've seen code that retains memory slowly, a little bit per alert, because a strong reference to `self` is captured by one of the blocks for the buttons, creating a memory cycle.

Thanks Quincy, thought so. I’ve been digging into instruments, in particular the leaks tool. It’s seems to give strange results, e.g. it says theres a memory leak in one place where the memory graph within Xcode says there isn’t. I think I need to read up a bit more.

Thanks again

Mark

Cheers Ed... I’ve used capture lists pretty much everywhere just in case. The next challenge is getting the map kit under control 🙂

IMHO, this specific point of UIAlertController retain cycle through handler sould qualify as a bug report. Adding a simple textfield to the alert and accessing it from the related handler create a retain cycle because the block retain the alert controller (left apart self, this is a direct cycle). The UIAlertController should be ehanced to release all actions and thus handlers blocks after the elected one returns, thus breaking the cycle. Sure the programmer should be aware, but nevertheless, this is a normal use case to be worked around in the framework.


There are probably already several bug reports (Are they ?) on that subject, so adding one may not be useful.

>Are they ?


File one and see what comes back...


As well, keep in mind that requests are used to help prioritize, so don't assume one more won't matter.


Feel free to make yours via the bug reporter link below right and good luck.

Never assume we know about an issue through many existing reports. Even if a bug report is returned to you as a duplicate, it is still useful to us.


Xcode 9.3 added a new compiler warning that helps with identifying retain cycle issues for Objective-C code. The setting is `CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF`, and is documented in the build settings reference. Projects created in older versions of Xcode will be prompted to turn this warning on when upgrading the project settings to the current recommended settings.

Simple memory question: UIKit
 
 
Q