Visual Memory Debugging with Xcode 8

Visual Memory Debugging with Xcode 8

Nowadays, there are tools that help us a lot to manage the memory when creating apps. Because of that, most software developers rarely encounter memory problems (or are simply unaware of them). However, every once in a while, we have to face some memory leaks. What do we do then? The most common method is to activate the Instruments tool and debug the problem, which can often be quite laborious. With the release of the new Xcode 8, Apple has introduced a new tool which significantly facilitates that – Visual Memory Debugger.

Creating a Leak

We will make a simple memory leak in order to test the new tool in practice. First, we create a simple class, the object of which may hold a reference to another object of the same class.

class LeakObject {
    var other: LeakObject?
}

Then, we create an equally simple view controller. It will hold a reference to a LeakObject. In the method viewDidLoad(), we will create another object of the same class and link them together.

class MemoryLeakViewController: UIViewController {
    
    @IBOutlet weak var button: UIButton!
    
    let firstObject = LeakObject()
    
    override func viewDidLoad() {
        super.viewDidLoad()


        let secondObject = LeakObject()
        
        firstObject.other = secondObject
        secondObject.other = firstObject
    }


    @IBAction func buttonAction(_ sender: AnyObject) {
        if let navVC = navigationController {
            navVC.popViewController(animated: true)
        }
    }


}

To facilitate this, I have created a button that will delete our view controller from the navigation stack. Everybody can probably figure out what should happen then. The mutual strong reference of the LeakObjects will allow them to remain in the memory, and a leak will occur. Let us move onto debugging, then!

Visual Memory Debugger

The icon launching the memory debugger is located to the right of the view debugger icon, which is quite frequently used by all iOS software developers.

Visual Memory Debugger icon on the panel

The debugger interrupts the execution of the app and displays a visualization of the memory usage. As we have suspected, deleting the view controller has caused a leak, of which the debugger has meticulously informed us.

Visual Memory Debugger - memory leak notofication

The main screen displays a visualization of those objects and their relations.

Visualization of the leak objects and their relations

Clicking a specific object will provide us with detailed information on it.

Detailed information about the leakObject

Furthermore, Xcode informed us of the leak while the program was running.

Debugger in Xcode

Clicking the purple icon provides us with detailed data on it.

Detailed data about the leak - view from Xcode

For the purpose of the article, a simple memory leak has been created and then meticulously spotted by Xcode and visualized by the new memory debugger. This tool will definitely be more useful for complicated problems, in larger projects.

The new debugger is intuitive and easy to use in iOS projects. I encourage you to use it at the nearest possible opportunity. You can find more information in Apple’s presentation from the WWDC that took place in June.

Learn more

How to Send iOS Notifications in Different Languages? Guide to Dynamic Localization

The best way to set a connection between a user, provider, and an app? System notifications. They allow users to get the latest news in no time. This solution is easy to implement in apps dedicated to one market. But it gets complicated when the messages must be displayed in many languages. In such cases, the dynamic localization of remote notifications can be a real game-changer.

Read more

WebSockets on iOS – Real-time Communication That Doesn’t Slow Down the App

When you want to download data for the app, you probably use the API RESTful interface. All it takes is to ask the server for the data and that’s it! This method works well when the app doesn’t need permanent access to new information. But what to do if the content has to be updated in real-time? A delay can make it impossible for the user to buy an item or make a sports bet, for example. But there’s a solution – WebSockets on iOS. Check out why you should implement them, and how to do it.

Read more

How to Implement Feature Flags and Gain More Control over Your App?

Every person with software development experience can share a few stories about incorrectly working features. We build our apps out of small pieces, introduce advanced architecture patterns, and yet sometimes one of the elements fails, causing bugs or even system failures. In such cases, feature toggling can save the day! Find out how to implement feature flags, and make your app more stable.

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2017

Nomination in
M-COMMERCE category

Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2020

Nomination in SPORTS & RECREATION category

18

client reviews

Clutch logo