0

I am testing my application and observing the memory. Initially, when the application has loaded the memory allocated in 15.7MB. I tap on a cell (using a UICollectionView) that triggers a segue to another view controller, that does have a delegate. I have made the delegate of type weak var. When the segue is complete and the view is presented the memory is now 87.6MB. However, when I hit the back button, the memory is still allocated.

This code is executed when the back button is pressed:

_ = navigationController?.popViewController(animated: true)

And will continue to increase by anywhere between 80mb-120mb each time I tap a collection view cell. And as a result, the application crashes. This issue only happens to this specific view/class/custom cell. This view/class/custom cell does indeed download and display images/videos from Firebase.

What can I do to tackle this issue?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Sounds like a reference cycle. Use the Debug Memory Graph feature of the Xcode debugger to find the cause. – rmaddy Apr 29 '19 at 00:43
  • @rmaddy I have tried the debugging tool, and I can see where the issue lies. It's just this view that display both media and text based content. I believe the value in never initialised to nil meaning it's always in memory. However, it is using a struct/model (not class) but I have not `init()` or `deinit()` methods. Do you think would help amend the issues> – TheApprentice Apr 29 '19 at 15:21
  • @Rob I have taken your suggest, if you could, please refer to my comment above as I have discovered some findings. – TheApprentice Apr 29 '19 at 15:21
  • Hmm. I see no evidence in your comment the you used the “Debug Memory Graph” option, which identifies strong reference cycles. And even if you didn’t, you didn’t share with us what that strong reference cycle was, nor share with us the relevant code. I’m really not trying to be difficult, but you simply haven’t shared enough details for anyone to help you. Regarding how to use the memory debugger, see WWDC 2016 video [Visual Debugging with Xcode](https://developer.apple.com/videos/play/wwdc2016/410/?time=1467). Or see https://stackoverflow.com/questions/30992338/30993476#30993476. – Rob Apr 29 '19 at 17:09
  • @Rob I have used the debugger tool after reading some documentation and follow tutorials. There are quite a large number of leaks and issues with allocations. However, whenever I select a value I cannot seem to get any indication as to what causes the issue(s) at hand. The responsible libraries for my leaked are `Foundation` which is only one leak and the rest are due to a library called `CFNetwork`. – TheApprentice Apr 30 '19 at 22:48
  • It’s a bit tough to help you in the abstract. In general, though, I’d advise ignoring foundation/CFNetwork types for now and focus solely on your classes. E.g. one leaked view controller will cause tons of Foundation objects to not get released, but there’s no point in worrying about anything but the leaked view controller. Are you 100% sure your pushed view controller, itself, is getting released? E.g. write a `deinit` method and add log statement and make sure the view controller is getting released. Also, maybe share screen snapshot of your “Debug Memory Graph” screen after having popped. – Rob Apr 30 '19 at 23:10
  • For us to provide substantive help, we really need a [MCVE](https://stackoverflow.com/help/mcve). E.g. make a copy of your project, and rip it down to the barebones of the problem. Figure out what this absolute minimum amount of code is necessary to reproduce the problem. (Often in that process of building the MCVE, you’ll figure out the problem yourself.) But no one here is going to want to pour through tons of unrelated code. You need to distill it down to a small, reproducible problem for us to help. – Rob Apr 30 '19 at 23:15
  • @Rob I am ever grateful for your help. So I wish to say thank you. My leaked view controller makes reference to a `Struct` model, this is why I believe the issue may lie, as a result, I have altered the model to a `Class` so I can use both a `init` and `deinit` methods. I will test to see if this can resolve the issue. But when using `Leaks` or `Allocations` there is no reference to the leaked view controller even though I push it several times. The memory increases, and is never released but cannot find a reference to the view controller. – TheApprentice May 01 '19 at 11:11

0 Answers0