When the view manager is rejected, should it clear the memory?

I would like to learn about memory management in Objective-C, which I find not so simple, because I'm pretty new to Objective-C and ARC, and I mostly use script languages ​​for which I don’t know how to deal with this (or not at all) with memory management.

The application I'm working on is a viewController (with an attached xib file) from the code after clicking a button. In this controller view, I have several views; I record a sequence of images (camera photos saved to disk) that I convert to a movie, and I have a gps tracker (mapKit) that displays a small map on the screen. In the end, I can click the "done" button, which calls [self dismissViewControllerAnimated:YES completion:nil];

The ViewController is animated back to my rootViewController and because I put the NSLog message inside the dealloc method in the viewController dispatcher, which is rejected, I can confirm that this viewController is freed.

The problem is that I see an increase in memory after using the application (the use consists of shooting and recording gps locations on a MapKit map, as well as creating a video file) to about 80 MB, and this drops to 70 MB when I click "done", so viewController is rejected and the application returns to my rootViewController. I can again introduce the same viewController, use it and fire it, and the application will still take about 70 MB of memory, which will not fall. Actually, this does not look like a memory leak, because in this case I would expect a constant memory growth with each instance and the dismissal of the viewController. This is not the case, even if I have different buttons in my root controller that all create a new and unique instance of my viewController class.

I am wondering: is there something I should look for or is this the expected behavior? Maybe the application caches classes for future use? With a memory management rule, should I expect the application to return to the state of "virgin" memory (in this case it will be about 4 MB) after firing the only viewController that was presented?

+5
source share
2 answers

If you see an increase in this memory in Xcode, and not when using tools, then the answer I found is given in this answer.

You can read the whole answer, this is pretty detailed. But a short story, in Xcode you see the amount of memory that the OS “gave” your application to use. This will increase at any time when your application is trying to highlight something. It doesn't decrease as fast because the OS speculates on performance, thinking that your application may need more memory in the future. The operating system quickly leaves this memory "given" to your application than to remove it and return it later.

+6
source

The view controller must be cleared from memory if it is not used by other objects (or does not have strong connections with other objects that have not yet been deleted from memory).

Ref.

 class ViewController: UIViewController { var secondViewController: SecondViewController? override func viewDidLoad() { super.viewDidLoad() } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let identifier = segue.identifier { if identifier == "second" { if secondViewController == nil { secondViewController = segue.destinationViewController as? SecondViewController } else { println("There still an instance of second view controller") } } } } } 

Suppose you first introduced secondViewController using a distribution layout and saved it in your ViewController instance as a property. Then you reject the secondViewController and submit the secondViewController from the ViewController again, it will print “There is still an instance of the second view controller”, since the one you presented earlier is still used by the ViewController.

However, when secondViewController has a weak connection, ( weak var secondViewController: SecondViewController ), it will never print, since the second ViewController will be deleted from memory and the secondViewController will be zero.

EDIT :

Note. The result should be the same when using Objective-C.

+2
source

All Articles