Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/facebook/FBRetainCycleDetector
iOS library to help detecting retain cycles in runtime.
https://github.com/facebook/FBRetainCycleDetector
Last synced: 3 months ago
JSON representation
iOS library to help detecting retain cycles in runtime.
- Host: GitHub
- URL: https://github.com/facebook/FBRetainCycleDetector
- Owner: facebook
- License: other
- Created: 2016-04-07T18:17:16.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2024-05-16T13:02:47.000Z (6 months ago)
- Last Synced: 2024-05-16T14:28:47.330Z (6 months ago)
- Language: Objective-C++
- Size: 2.91 MB
- Stars: 4,191
- Watchers: 108
- Forks: 578
- Open Issues: 42
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-ios - FBRetainCycleDetector - iOS library to help detecting retain cycles in runtime. (Code Quality)
- awesome-ios-star - FBRetainCycleDetector - iOS library to help detecting retain cycles in runtime. (Code Quality)
- Awesome-iOS - FBRetainCycleDetector - An iOS library that finds retain cycles using runtime analysis. (Tools)
- fucking-awesome-ios - FBRetainCycleDetector - iOS library to help detecting retain cycles in runtime. (Code Quality)
- fucking-awesome-ios - FBRetainCycleDetector - iOS library to help detecting retain cycles in runtime. (Code Quality)
README
# FBRetainCycleDetector
[![Support Ukraine](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB)](https://opensource.fb.com/support-ukraine)
[![Build Status](https://travis-ci.org/facebook/FBRetainCycleDetector.svg?branch=main)](https://travis-ci.org/facebook/FBRetainCycleDetector)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![CocoaPods](https://img.shields.io/cocoapods/v/FBRetainCycleDetector.svg)](https://cocoapods.org/pods/FBRetainCycleDetector)
[![License](https://img.shields.io/cocoapods/l/FBRetainCycleDetector.svg)](https://github.com/facebook/FBRetainCycledetector/blob/main/LICENSE)An iOS library that finds retain cycles using runtime analysis.
## About
Retain cycles are one of the most common ways of creating memory leaks. It's incredibly easy to create a retain cycle, and tends to be hard to spot it.
The goal of FBRetainCycleDetector is to help find retain cycles at runtime.
The features of this project were influenced by [Circle](https://github.com/mikeash/Circle).## Installation
### Carthage
To your Cartfile add:
github "facebook/FBRetainCycleDetector"
`FBRetainCycleDetector` is built out from non-debug builds, so when you want to test it, use
carthage update --configuration Debug
### CocoaPods
To your podspec add:
pod 'FBRetainCycleDetector'
You'll be able to use `FBRetainCycleDetector` fully only in `Debug` builds. This is controlled by [compilation flag](https://github.com/facebook/FBRetainCycleDetector/blob/main/FBRetainCycleDetector/Detector/FBRetainCycleDetector.h#L83) that can be provided to the build to make it work in other configurations.
## Example usage
Let's quickly dive in
```objc
#import
``````objc
FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
[detector addCandidate:myObject];
NSSet *retainCycles = [detector findRetainCycles];
NSLog(@"%@", retainCycles);
````- (NSSet *> *)findRetainCycles` will return a set of arrays of wrapped objects. It's pretty hard to look at at first, but let's go through it. Every array in this set will represent one retain cycle. Every element in this array is a wrapper around one object in this retain cycle. Check [FBObjectiveCGraphElement](https://github.com/facebook/FBRetainCycleDetector/blob/main/FBRetainCycleDetector/Graph/FBObjectiveCGraphElement.h).
Example output could look like this:
```
{(
(
"-> MyObject ",
"-> _someObject -> __NSArrayI "
)
)}
```
`MyObject` through `someObject` property retained `NSArray` that it was a part of.FBRetainCycleDetector will look for cycles that are no longer than 10 objects.
We can make it bigger (although it's going to be slower!).```objc
FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
[detector addCandidate:myObject];
NSSet *retainCycles = [detector findRetainCyclesWithMaxCycleLength:100];
```### Filters
There could also be retain cycles that we would like to omit. It's because not every retain cycle is a leak, and we might want to filter them out.
To do so we need to specify filters:```objc
NSMutableArray *filters = @[
FBFilterBlockWithObjectIvarRelation([UIView class], @"_subviewCache"),
];// Configuration object can describe filters as well as some options
FBObjectGraphConfiguration *configuration =
[[FBObjectGraphConfiguration alloc] initWithFilterBlocks:filters
shouldInspectTimers:YES];
FBRetainCycleDetector *detector = [[FBRetainCycleDetector alloc] initWithConfiguration:configuration];
[detector addCandidate:myObject];
NSSet *retainCycles = [detector findRetainCycles];
```Every filter is a block that having two `FBObjectiveCGraphElement` objects can say, if their relation is valid.
Check [FBStandardGraphEdgeFilters](FBRetainCycleDetector/Filtering/FBStandardGraphEdgeFilters.h) to learn more about how to use filters.
### NSTimer
NSTimer can be troublesome as it will retain it's target. Oftentimes it means a retain cycle. `FBRetainCycleDetector` can detect those,
but if you want to skip them, you can specify that in the configuration you are passing to `FBRetainCycleDetector`.```objc
FBObjectGraphConfiguration *configuration =
[[FBObjectGraphConfiguration alloc] initWithFilterBlocks:someFilters
shouldInspectTimers:NO];
FBRetainCycleDetector *detector = [[FBRetainCycleDetector alloc] initWithConfiguration:configuration];
```### Associations
Objective-C let's us set associated objects for every object using [objc_setAssociatedObject](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/#//apple_ref/c/func/objc_setAssociatedObject).
These associated objects can lead to retain cycles if we use retaining policies, like `OBJC_ASSOCIATION_RETAIN_NONATOMIC`. FBRetainCycleDetector can catch these kinds of cycles, but to do so we need to set it up. Early in the application's lifetime, preferably in `main.m` we can add this:
```objc
#importint main(int argc, char * argv[]) {
@autoreleasepool {
[FBAssociationManager hook];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
```In the code above `[FBAssociationManager hook]` will use [fishhook](https://github.com/facebook/fishhook) to interpose functions `objc_setAssociatedObject` and `objc_resetAssociatedObjects` to track associations before they are made.
## Getting Candidates
If you want to profile your app, you might want to have an abstraction over how to get candidates for `FBRetainCycleDetector`. While you can simply track it your own, you can also use [FBAllocationTracker](https://github.com/facebook/FBAllocationTracker). It's a small tool we created that can help you track the objects. It offers simple API that you can query for example for all instances of given class, or all class names currently tracked, etc.
`FBAllocationTracker` and `FBRetainCycleDetector` can work nicely together. We have created a small example and drop-in project called [FBMemoryProfiler](https://github.com/facebook/FBMemoryProfiler) that leverages both these projects. It offers you very basic UI that you can use to track all allocations and force retain cycle detection from UI.
## Contributing
See the [CONTRIBUTING](CONTRIBUTING.md) file for how to help out.## License
[`FBRetainCycleDetector` is BSD-licensed](LICENSE).