22

Is there a way to quickly log out the retain count of objects to Xcode's Console? If not, what's the next best alternative?

sirab333
  • 3,502
  • 8
  • 37
  • 54
  • 5
    Why you need retainCount ? – Midhun MP Mar 10 '15 at 20:06
  • 1
    Just as a teaching tool. I wanna show students how strong retain cycles happen, and just talk about the whole ARC thing. – sirab333 Mar 10 '15 at 21:10
  • Read these before using `retainCount` method. [10 (http://whentouseretaincount.com), [2](http://stackoverflow.com/questions/4636146/when-to-use-retaincount), [3] (http://www.friday.com/bbum/2011/12/18/retaincount-is-useless/), [4](http://stackoverflow.com/questions/5784084/calling-retaincount-considered-harmful), [5 Apple's explanation on retainCount method](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/#//apple_ref/occ/intfm/NSObject/retainCount) – Midhun MP Mar 10 '15 at 21:15
  • well it was never my intention to use `retainCount`. I don't think it works in Swift in the first place, plus the docs say its not reliable. Just thought there'd be some simple way... – sirab333 Mar 10 '15 at 21:18
  • AFAIK there is no other way other than using XCode tools. – Midhun MP Mar 10 '15 at 21:26
  • 1
    @sirab333 There is no difference between `retainCount` and `CFGetRetainCount()`. A far better method of teaching about cycles is to use boxes and lines to draw the object graph; the retain counts are meaningless. – bbum Jun 25 '16 at 13:58
  • 8
    We could lose the puritanism, guys. If he wants the retainCount, he wants the retainCount. Why he wants it is really none of your business. He wasn't asking for best memory management practices. – Womble Jun 06 '18 at 02:20

3 Answers3

49

using CFGetRetainCount function

Example:

// `CFGetRetainCount` is only available in the `Foundation` module
import Foundation

print(CFGetRetainCount(object))

Read more here : https://developer.apple.com/reference/corefoundation/1521288-cfgetretaincount

hope helpful

Max Desiatov
  • 3,514
  • 2
  • 40
  • 49
Tritmm
  • 1,477
  • 15
  • 12
  • 3
    Also note that in Swift, `CFRetainCount` also counts references of 'unknown' type, which could include weak references. I learned this by cross-referencing the runtime `CFGetRetainCount` value with the Xcode "View Memory Graph Hierarchy" tool. More here: https://koenig-media.raywenderlich.com/uploads/2017/08/visual_memory_debugger.png – AtomicBoolean Oct 20 '17 at 14:22
1

Typically you would use instruments to get the retain count. But as answered here the method is retainCount.

How to get the reference count of an NSObject?

Community
  • 1
  • 1
InkGolem
  • 2,502
  • 1
  • 12
  • 21
  • 2
    `retainCount` is no good in Swift. And otherwise - look at the docs under "Special Considerations": "This method is of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it..." How would you do it in Instruments? – sirab333 Mar 10 '15 at 21:16
  • Don't expect retainCount give you the exact values. Refer the links I attached to my comment (Question section) – Midhun MP Mar 10 '15 at 21:16
-3
print(_getRetainCount(objectName))
Subhajit
  • 5
  • 1