简体   繁体   中英

Memory usage increases with every segue to modal vc

I have two view controllers, one is the timeline the second one is for the creation. In that second view controller I have a sub view. This sub view is an SKView . Now every time I segue to it, it increases the memory usage by 2mb (on a real device), but the memory usage stays the same when I unwind it.
So it is like this: I start with a usage of 12mb, then it gets 14-15mb. After the unwind it stays around 14-15mb. After the second segue to it, it becomes 17mb... and so on.

This is the code used in the timeline controller:

@IBAction func createButtonAct(sender: AnyObject) {

    self.performSegueWithIdentifier("create", sender: self)


}

@IBAction func unwindFromCreation(segue: UIStoryboardSegue) {


}




// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "create"{
        let vc = segue.destinationViewController as! CreateViewController
        if vc.currCountry == nil || vc.currCountry != self.currCountry{
        vc.currCountry = self.currCountry
        }

    }


}

And this is the code in the create View Controller:

class CreateViewController: UIViewController, UITextViewDelegate {

@IBOutlet weak var bubbleView: SKView!

@IBOutlet var arrow: UIButton!

var ref: Firebase!


let categories = CatsAndColors.categories


@IBOutlet var doneButton: UIButton!

@IBOutlet var titleField: UITextField!

@IBOutlet var descriptionView: KMPlaceholderTextView!

var choosedCat: String!

var selectedCats: NSMutableArray!

var currCountry:String!

var tap: UITapGestureRecognizer!

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

    UIApplication.sharedApplication().statusBarStyle = .Default
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        self.arrow.transform = CGAffineTransformMakeRotation(3.14159)
    })



    titleField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged) 

    titleField.addTarget(self, action: "textFieldDidBegin:", forControlEvents: UIControlEvents.EditingDidBegin)

    titleField.addTarget(self, action: "textFieldDidEnd:", forControlEvents: UIControlEvents.EditingDidEnd)
    // the targets get removed in viewWillDisappear
    selectedCats = NSMutableArray()

}



override func viewDidLoad() {
    super.viewDidLoad()

    ref = Firebase(url: "https://blabber2.firebaseio.com")

    self.doneButton.enabled = false

    doneButton.setBackgroundImage(UIImage(named: "Done button inactive"), forState: .Disabled)

    doneButton.setTitleColor(UIColor(netHex: 0xF6F6F6), forState: .Disabled)

    doneButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)

    self.setupBubbles()

    self.descriptionView.delegate = self

}

func setupBubbles(){



    let floatingCollectionScene = ChooseBubblesScene(size: bubbleView.bounds.size)

    // floatingCollectionScene.scaleMode = .AspectFit


    /*let statusBarHeight = CGRectGetHeight(UIApplication.sharedApplication().statusBarFrame)
    let titleLabelHeight = CGRectGetHeight(self.tibleLabel.frame)*/

    bubbleView.presentScene(floatingCollectionScene)



    for (category, color) in categories {

        let node = ChooseBubbleNode.instantiate()
        node!.vc = self
        node!.fillColor = SKColor(netHex: color)
        node!.strokeColor = SKColor(netHex: color)
        node!.labelNode.text = category

        floatingCollectionScene.addChild(node!)

    }


}
...

And the catsAndColors struct looks like this:

struct CatsAndColors{

static var categories = ["Crime":0x5F5068, "Travel":0xFBCB43, "Religion":0xE55555, "Tech":0xAF3151, "Economy":0x955BA5, "Games":0xE76851, "Climate":0x6ED79A, "Books":0xE54242, "History":0x287572, "Clothes":0x515151, "Sports":0x4AB3A7, "Food":0xD87171, "Politics":0x5FA6D6, "Music":0xDD2E63, "Tv-shows":0x77A7FB]

 }

Maybe you have created some sort of retain cycle between your view controllers.
If both view controllers hold a reference to each other, then try declaring one of the references as weak .

For more information on the topic read Resolving Strong Reference Cycles Between Class Instances .

I solved the problem, it was strong reference in the sknode file.

Thank you for your answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM