How do you detect memory leaks on iPhone?

2019-01-16 06:34发布

I'm using the Leaks Instruments feature through Xcode to (try and) find memory leaks. I still haven't figured out how to use this program. I click Leaks in the program and see memory increasing as I do various things in the simulator. I have Extended Detail pane displayed. The only thing in Extended Detail pane that references my app is main. As in the main method produced by Xcode. Everything else is UIKit, Foundations, and other SDK classes I didn't write. What am I doing wrong that nothing is showing up from my app?

Before I hit 3 minutes, there are over 100 leaks totaling 2.5k. Is this common?

12条回答
混吃等死
2楼-- · 2019-01-16 07:26

Xcode: run -> Start with Performance Tool -> Leaks

查看更多
Rolldiameter
3楼-- · 2019-01-16 07:28

Note also that the leak tool is not going to show you instances where objects are over-retained and still held on to. Leaks are cases where objects that should have been let go are just hanging around with no-one to clean them up. Over retained objects are validly held onto even though you'd think they should be gone - thus the leak tool cannot point them out, since they are still referred to and there's no way to tell them apart from objects that should still be retained.

To find those, use the memory reporting tool and make sure that memory use goes down fully after you free an object. If you notice something isn't freeing memory, you can start by putting breakpoints in dealloc to see if what you expect to see released is actually getting released.

You need to look for both cases to keep a clean memory footprint.

查看更多
Emotional °昔
4楼-- · 2019-01-16 07:29

One of the best way to find the memory leaks is Select Product-> Analyze. In the left Xcode shows in which file you have memory leaks. What are the variable causing memory leaks. This is one of the best way to find memory leaks.

查看更多
▲ chillily
5楼-- · 2019-01-16 07:32

I'm not familiar with how to use Leaks, but you can always try running the Clang analyzer on your code to see if that'll turn anything up: http://clang.llvm.org/StaticAnalysis.html. It can often find many bugs that might lead to memory leaks.

查看更多
趁早两清
6楼-- · 2019-01-16 07:32

Use LLVM/Clang Static Analyzer.

查看更多
家丑人穷心不美
7楼-- · 2019-01-16 07:39

To detect memory leaks you can use the "build and analyze" function of Xcode.

Simply select Build -> Build and Analyze in the Xcode menu.

查看更多
登录 后发表回答