私は初めてiOS/Objective-C
で、メモリの解放を正しく理解していません。それをテストするために、空のARC
有効な iPhone プロジェクトを作成し、非常に単純なテスト クラスを作成しました。
#import "MemTest.h"
@implementation MemTest {
}
-(void) start {
for (int i = 0; i < 1500000; i++) {
NSMutableString *myString = [NSMutableString string];
// The appended string is 2000 characters long in the real test class.
[myString appendString:@"12345678901234567890123456 <very long>"];
if (i % 1000 == 0) {
NSLog(@"i = %d", i);
}
myString = nil;
}
}
@end
次の場所でテストを開始するだけですAppDelegate
。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MemTest *test = [[MemTest alloc] init];
[test start];
....
}
アプリケーションは (予想どおり) 多くの適切な数値 "i = xy" を出力しますが、反復ごとにメモリ使用量が増加し、最終的にアプリケーションがクラッシュします。
....
2012-12-06 20:17:40.193 MemTestApp[19250:11303] i = 930000
2012-12-06 20:17:40.208 MemTestApp[19250:11303] i = 931000
MemTestApp(19250,0xac63f2c0) malloc: *** mmap(size=16777216) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
だから私の質問は:メモリ使用量がまったく増加しているのはなぜですか?
を使用する場合、nilを割り当てることでメモリを解放する必要があると思いましたARC
。ここで何が欠けていますか?