![]() ![]() In order for an object to be removed from memory, its reference count must be zero no objects should be referencing that address in memory. This means that the retain count for a value type is typically always one there is never more than one object pointing to the memory address of a value type. When you're passing around value types, the value is copied when it's passed around. Because when you pass around an instance of a class in your code, you're really passing around a memory reference, which means that multiple objects point to the same memory address. This principle of retain counts mostly applies to classes. Any time you pass a reference to your object to a different place in your code, its retain count is increased because there is now one more object pointing at the location in memory for that object. ![]() Every instance of an object you create in your app has a retain count. In iOS, objects are removed from memory when there are no other objects that keep a strong reference to them. Understanding how retain cycles occur and how to fix them Let's see how this occurs and what you can do to fix it. The DataProvider and DetailPage in this example are pointing at each other. When you click one of them, the problem becomes visible immediately. The screenshot above shows that instances of two different objects are kept in memory while Xcode thinks they shouldn't. If they are more hidden, or you just want to filter memory leaks, you can do so using the filter menu at the bottom of the sidebar as shown in the following screenshot: In the screenshot you just saw, it's quite obvious where the purple squares are. If Xcode spots a relationship that it suspects to be a memory leak, or retain cycle, it will add a purple square with a exclamation mark behind the object in the sidebar. Other times it's a smaller graph with just a couple of objects. ![]() Sometimes it's a big graph, like in the screenshot. When you select an object in the sidebar, the middle section will show your selected object, and the relationships it has to other objects. In the sidebar on the left-hand side, Xcode shows a full list of all objects that it has discovered. Note that this might take a little while, depending on how big your app is. Your app's execution will be paused and Xcode will show you all objects that are currently in memory. When you click this icon, Xcode will take a snapshot of your app's memory graph and the relationships that every object has to other objects. When you run your app with Xcode, you can click the memory debugger icon that's located between your code and the console, or at the bottom of your Xcode window if you don't have the console open: I will focus specifically on retain cycles today. In today's post, I will show you how to use the Memory Graph tool in Xcode to analyze the objects that are kept in memory for your app, and how to use this tool to discover memory leaks. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |