facebookarchive / FBMemoryProfiler

iOS tool that helps with profiling iOS Memory usage.
Other
3.41k stars 377 forks source link

Misjudged on retained cycle #35

Closed without2002 closed 7 years ago

without2002 commented 7 years ago

Hi, we use FBMemoryProfiler in my project, but I found some misjudged retained cycle. In the code, using FBObjectiveCGraphElement to save the information of retain cycled object. But the property of FBObjectiveCGraphElement object is weak. When I checked a retained cycle, and notify the observers, at the same time, the retained cycle have been broken, the object was deallocated. Finally the observers receive a retained cycle chain like this: ( "-> _traitCollection -> (null) ", "-> _image -> (null) ", "-> _borderImageHL -> (null) " ) I think it is not retained cycle. I don't know if I have some wrong in the analysis. Could you check it?

Gricha commented 7 years ago

Hey there. You are right that graph elements will be keeping objects weakly and that's by design. We wouldn't want retain cycle to cause additional memory footprint to arise.

So the thing you are seeing here is a retain cycle that actually happened, but was broken down. Many retain cycles are being created intentionally and then properly invalidated. RCD has no way of recognizing these cases so in some cases it will return elements will null in it. If that's the case you should feel free to ignore that. I'm also happy to accept a pull request that would filter them out before returning these. The original issue here was though that since the references are weak, this nulls can show up at any time.

The only guarantee you get is that if the tool returns retain cycle at least it has been there at some point.

Let me know if you have more questions.