ここで説明されている手法を使用して、アプリのメモリ使用量を監視し始めました: プログラムで iPhone のメモリ使用量を取得します。
私はそれを試すために3つのテストを書きましたが、これが私が見つけたものです:
- (void)test1 {
for (int i = 0; i < 1000; i++) {
NSMutableString *str = [NSMutableString stringWithString:@""];
for (int j = 0; j < 1000; j++) {
[str appendString:@"some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string "];
}
}
- (void)test2 {
for (int i = 0; i < 100000; i++) {
@autoreleasepool {
NSString *stri = @"";
stri = [NSString stringWithFormat:@"%d some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really", i];
}
}
- (void)test3 {
NSString *str = @"";
for (int i = 0; i < 500; i++) {
str = [str stringByAppendingFormat:@"%d some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really", i];
}
test1 または test3 を呼び出すと、メモリが適切に割り当てられ、割り当てが解除されます - 上記のリンクで説明されている report_memory 関数を使用して確認できます。しかし、test2 を呼び出すと、メモリの割り当てが解除されません。report_memory がどんどん増えていきます。test2 を数回呼び出すと、アプリがメモリ警告を受け取り、終了します。
ARCを使用しています。ここで何が起こっているのか誰か説明できますか?