LeftyT
LeftyT

Reputation: 522

Swift - garbage collection - something gets left behind

I'm fairly new to iOS development, so I apologize in advance if my question sounds not worthy of being on stackoverfow.

I am building a simple game with a home ViewController and a gameplay ViewController as far as the structure is concerned.

I've added a very simple function to kill activities to make sure nothing gets left behind, which gets called right before it leaves the gameplay ViewController. the following is the code I'm using:

private func cleanThis(){  

   //removing objects from array
   activeEnemies.removeAll()

   //removing objects from array
   activeTargets.removeAll()

   //removing the rest
   let subViews = self.view.subviews
   for subview in subViews{
      subview.removeFromSuperview()
   }
}

I must be missing something here, and when I test it without moving on to the homepage, there's a constant memory activity of 38MB. And I haven't figured out yet as to how to monitor what is still left in the ViewController.

Any help is much appreciated.

p.s. when there's no objects in ViewController, the memory activity should be 0, is this correct?

Upvotes: 1

Views: 2765

Answers (1)

David S.
David S.

Reputation: 6705

iOS doesn't use a garbage collector. It uses a something called automatic reference counting. The main way you get into trouble is cyclical references (A has a strong reference to B which has a strong reference to A).

Xcode Instruments will show you all memory allocations and deallocations, and can show memory leaks as well. Here's a screenshot to demonstrate:

Instruments - Leak Check

Upvotes: 3

Related Questions