After the application develops to a certain scale, various memory problems occur frequently and it is difficult to locate. Have you experienced this kind of pain?
Memory problems
As our project grew in size, the code structure became more complex. At this point many memory problems become more and more difficult to solve. An inadvertent circular reference can result in a portion of memory being used forever. Such memory leaks are often introduced into projects as the amount of code increases. At the end of the day, the problem often falls into disuse.
It’s good that phones now have more and more memory, but even so, as your projects get bigger and bigger, these memory problems will definitely have more and more impact on the stability of your applications.
FBMemoryProfiler
Because of these problems, today I introduce FBMemoryProfiler, an open source library for Facebook. Github.com/facebook/FB…
Circular references are the main culprit of memory leaks in our applications. Circular references are instances that use strong references to each other, so that no one can free the other.
FBMemoryProfiler is a combination of several components. These include FBAllocationTracker and FBRetainCycleDetector.
FBAllocationTracker is used to detect the allocation of all instances of the application at run time. The idea is to replace the alloc method with method Swizzling. This allows you to keep track of all instance assignments. It is also not difficult to use, and is initialized in the main function of the project:
#import int main(int argc, char * argv[]) { [[FBAllocationTrackerManager sharedManager] startTrackingAllocations]; [[FBAllocationTrackerManager sharedManager] enableGenerations]; @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }}Copy the code
The currentAllocationSummary method is then called when needed to get the current overall instance allocation:
NSArray<FBAllocationTrackerSummary *> *summaries = [[FBAllocationTrackerManager sharedManager] currentAllocationSummary];Copy the code
With the FBAllocationTracker, we can keep track of all the sample assignments in our application. However, another component, FBRetainCycleDetector, is required to complete the detection of circular references.
The FBRetainCycleDetector takes an instance of the runtime and starts from that instance iterating through all of its properties, level by level. Circular references exist and are reported if repeated instances are found traversed.
FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
[detector addCandidate:myObject];
NSSet *retainCycles = [detector findRetainCycles];
NSLog(@"%@", retainCycles);Copy the code
The code above initializes the FBRetainCycleDetector instance and then calls the addCandidate method to add the instance to be detected. The findRetainCycles method is then called to detect the cyclic reference and output it.
If there is a circular reference, something like this is printed:
{(
(
"-> MyObject ",
"-> _someObject -> __NSArrayI "
)
)}Copy the code
Using this clue, you can find areas in your code that might cause circular references.
With the two basic components covered, FBMemoryProfiler is next. It actually calls these two components internally and displays them graphically, just by calling them like this to display the UI:
#import
FBMemoryProfiler *memoryProfiler = [FBMemoryProfiler new];
[memoryProfiler enable];
// Store memory profiler somewhere to extend it's lifetime
_memoryProfiler = memoryProfiler;
Copy the code
Then start the application and the full memory report is displayed:
conclusion
In addition to such manual debugging, FBMemoryProfiler can also perform automated detection. With its two built-in components, FBRetainCycleDetector and FBAllocationTracker, it can directly detect the circular references in memory and then send the data to its own server, forming an automated detection system. Facebook has an article about this, everyone interested in can also study the code.facebook.com/posts/58394… . If you need to optimize memory references in depth, this tool should help you.
FBMemoryProfiler’s Github home page: github.com/facebook/FB…
If you find this article helpful, you can also follow the wechat official account Swift-cafe and I will share more of my original content with you