Wednesday, April 27, 2011

How to check memory leaks in XCode 4 ?

You need to go to Product>Profile on the top menu of XCode 4 as shown in screenshot below.




























Then you will see following window, you can just choose Leaks and Press Profile button, it will start the Instrument and start tracking leaks and allocation.






































As soon as you press Profile, it will start Instrument and the simulator/device(You can change in the settings of the Instrument whether you want to run it on simulator or device) will start running the app.
You scroll through various pages on your app. While doing so,  if you view the red line in Leaks windows of Instrument as shown below then you have memory leak detected ! You can move the dotted line with slider to find out how many bytes leaked at what time in a small yellow pop up.































When you double click the red thick line of memory leak as shown  above , in the lower portion of the Instrument you will be able to see all the leaks and related info. If I want to find detailed info about a particular leak,  in this tutorial as you can see the blue highlighted line of leak, I am going to inspect about that leak hence I will just click the arrow which comes up in its address field.










This will refresh the window and tell you the details with  Responsible Caller column in the end of the table , it gives the specific method  name where the leaks has occurred. If you double click the row, it will open that corresponding .m file where that method has been implemented.


































Now you can see that the line is highlighted where the leak has occurred in the method of a .m file in the project.










 Hope this helps to someone new in fixing memory leaks !

10 comments:

  1. Thanks so much! You're the first tutorial on the web for using instruments in xcode 4! Very helpful

    ReplyDelete
  2. Thanks :) Let me know if you have any suggestions to make it better :) I will appreciate it.

    ReplyDelete
    Replies
    1. the last step is not working for me. When i am making a double click on the row, it is not happening.

      Delete
  3. hi reetu, how can i identify the object when it's over-released n i got the following message ? GDB: Program received signal: "EXC_BAD_ACCESS

    thx :)

    ReplyDelete
  4. the last step If you double click the row, it will open that corresponding .m file where that method has been implemented. is not working for me..i am using xcode 4 only

    ReplyDelete
  5. In Xcode 4 to see the line where the method is implemented, first use the little arrow to go to the detail view for that leak, then open the 'extended view' right-hand pane using the button in the toolbar. In that pane you will see a stack trace - each item in the trace takes you to the relevant line when double-clicked.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. Android SDK allows you to create stunning mobile application loaded with more features and enhanced priority. With basis on Java coding language, you can create stunning mobile application with ease.

    Cado Magenge
    "http://appdevelopmentcompany.com.au/android-application-development.html"
    "http://www.appdevelopmentcompany.com.au/email-marketing.html"
    "http://appdevelopmentcompany.com.au/iphone-application-development.html"
    ”http://appdevelopmentcompany.com.au/ipad-application-development.html”

    ReplyDelete