新QQ交流群:418536818,之前的群已满
操作如下:
1、增加NSZombieEnabled和MallocStackLogging环境变量:菜单Product--》Edit Scheme...,在弹出的窗口上方选择自己的项目及模拟器,左侧选择"Run ***.app"(***是你的项目名称),右侧选择Arguments选项卡,在Environment Variables中增加Name: NSZombieEnabled--》Value:YES、Name: MallocStackLogging--》Value:YES,并点击2个环境变量左侧的复选框,保证在启用状态。
2、debug自己的项目,出现EXC_BAD_ACCESS错误的时候,在控制台会出现类似下面的log
fbft(1431) malloc: process 1249 no longer exists, stack logs deleted from /tmp/stack-logs.1249.fbft.bBsBO7.index
fbft(1431) malloc: stack logs being written into /tmp/stack-logs.1431.fbft.Fz32vw.index
2012-01-18 11:38:55.118 fbft[ 1431:ef03] *** -[_ UIResizableImage release]: message sent to deallocated instance 0x7245470
从最后一行可以看出是调用了已经被dealloc的对象。(UIResizableImage应该是已经release的对象类型)
3、在控制台的(gdb)后面输入shell malloc_history 1431 0x7245470(其中1431和0x7245470是上面log中的pid和address),并回车。得到如下信息
ALLOC 0x7245470-0x72454af [size=64]: thread_b024f000 |thread_start | _pthread_start | _ZN2KBL14BackgroundLoadEPv | KB::DynamicDictionaryImpl::background_load_address_book(KB::StaticDictionary const&) | KB::fill_with_matchable_strings_from_address_book(KB::Hashmap<KB::String, bool>&) | ABAddressBookCreate | ABCCreateAddressBookWithDatabaseDirectory | ABCCreateAddressBookWithDatabaseDirectoryAndForceInProcessMigrationInProcessLinkingAndResetSortKeys | ABCDBContextCreateWithPathAndAddressBook | CPSqliteDatabaseRegisterFunction | CPSqliteDatabaseConnectionForWriting | _connectAndCheckVersion | _createConnectionForWriting | CPSqliteConnectionPerformSQL | CPSqliteConnectionStatementForSQLAndIgnoreErrors | CPSqlitePreparedStatement | sqlite3_prepare_v2 | sqlite3LockAndPrepare | sqlite3Prepare | sqlite3RunParser | sqlite3Parser | yy_reduce | sqlite3Pragma | sqlite3Init | sqlite3InitOne | sqlite3_exec | sqlite3InitCallback | sqlite3LockAndPrepare | sqlite3Prepare | sqlite3RunParser | sqlite3Parser | yy_reduce | sqlite3ExprAlloc | sqlite3DbMallocRaw | sqlite3MemMalloc | malloc_zone_malloc
----
FREE 0x7245470-0x72454af [size=64]: thread_b024f000 |thread_start | _pthread_start | _ZN2KBL14BackgroundLoadEPv | KB::DynamicDictionaryImpl::background_load_address_book(KB::StaticDictionary const&) | KB::fill_with_matchable_strings_from_address_book(KB::Hashmap<KB::String, bool>&) | ABAddressBookCreate | ABCCreateAddressBookWithDatabaseDirectory | ABCCreateAddressBookWithDatabaseDirectoryAndForceInProcessMigrationInProcessLinkingAndResetSortKeys | ABCDBContextCreateWithPathAndAddressBook | CPSqliteDatabaseRegisterFunction | CPSqliteDatabaseConnectionForWriting | _connectAndCheckVersion | _createConnectionForWriting | CPSqliteConnectionPerformSQL | CPSqliteConnectionStatementForSQLAndIgnoreErrors | CPSqlitePreparedStatement | sqlite3_prepare_v2 | sqlite3LockAndPrepare | sqlite3Prepare | sqlite3RunParser | sqlite3Parser | yy_reduce | sqlite3Pragma | sqlite3Init | sqlite3InitOne | sqlite3_exec | sqlite3InitCallback | sqlite3LockAndPrepare | sqlite3Prepare | sqlite3RunParser | sqlite3Parser | yy_reduce | sqlite3ExprDelete | sqlite3ExprDelete | sqlite3ExprDelete | sqlite3ExprDelete | sqlite3ExprDelete | sqlite3ExprListDelete | sqlite3ExprDelete | sqlite3DbFree | sqlite3MemFree | malloc_zone_free
ALLOC 0x7245470-0x7245493 [size=36]: thread_ad12a2c0 |start | main | UIApplicationMain | -[UIApplication _run] | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSource1 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ | PurpleEventCallback | _UIApplicationHandleEvent | -[UIApplication sendEvent:] | -[UIApplication handleEvent:withNewEvent:] | -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] | -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] | -[FbftAppDelegate application:didFinishLaunchingWithOptions:] | -[UIWindow makeKeyAndVisible] | -[UIWindow _orderFrontWithoutMakingKey] | -[UIWindow _setHidden:forced:] | -[UIWindow addRootViewControllerViewIfPossible] | -[UIViewController view] | -[ TestViewController viewDidLoad] | -[UIImage(UIImageDeprecated) stretchableImageWithLeftCapWidth:topCapHeight :] | -[UIImage(UIImagePrivate) _stretchableImageWithCapInsets:] | -[UIImage resizableImageWithCapInsets:] | _objc_rootAlloc | +[NSObject allocWithZone:] | _objc_rootAllocWithZone | class_createInstance | calloc | malloc_zone_calloc
Binary Images:
0x1000 - 0x3ffb +fbft (??? - ???) <92BCD2F3-83ED-36DD-A56A-37022E0229E0> /Users/fangjiawang/Library/Application Support/iPhone Simulator/5.0/Applications/8ED06429-A51E-4D6C-9ACC-C9A6E269C80A/fbft.app/fbft
0x7000 - 0x612fff +UIKit (1600.0.0 - comp
.......
从上面标红的log中可以大概知道是哪里的代码出了问题,TestViewController类中的stretchableImageWithLeftCapWidth:topCapHeight方法中引入了已经release对象。
当我们把将非ARC的内存管理都管理好后,发现在做有些操作的时候内存还是在一直的缓慢增加 比如做一个最简单的随机数UITableView的显示与滑动,进行内存管理后,不应该出现内存增加的,但是一直滑动内存就一直缓慢的往上增加的情况。
这时候我们可以检查下看这里的属性是否打勾:
如果勾上,上面三个选项,控制台就会出现下列几行输出
ARCTest(651,0x1f321a8) malloc: stack logs being written into /tmp/stack-logs.651.8af7000.ARCTest.jGqgoD.indexARCTest(651,0x1f321a8) malloc: recording malloc and VM allocation stacks to disk using standard recorder
如果发现有的话,把图1的三个属性的勾去掉。这三个选项只要用户在不断的操作,程序就会记录下一些东西,这个时候就会耗费一定的内存。