我使用以下代码创建了一个数组。在不再需要 12 张图像后,我将 imageArray 设置为 nil 并将一组新图像重新加载到该数组中。当我在仪器中运行应用程序时,我可以看到内存累积问题。我还运行了 heapshots,它显示即使我将数组设置为 nil 后仍有 12 个图像仍然存在。我还尝试在它自己的自动释放池中初始化这个数组,认为它是在主线程下方的一个单独线程上创建的。那也没有用。有任何想法吗?
视图控制器.h
@property (strong, nonatomic) NSMutableArray *imageArray;
视图控制器.m
- (void) firstSetOfImages{
imageArray= [[NSMutableArray alloc] initWithObjects:wordImage1, wordImage2, wordImage3, wordImage4, wordImage5, wordImage6, wordImage7, wordImage8, wordImage9, wordImage10, wordImage11, wordImage12, nil];
}
- (void) clearImages{
[self setImageArray:nil];
[imageArray removeAllObjects];
[self secondSetOfImages];
}
- (void) secondSetOfImages{
imageArray= [[NSMutableArray alloc] initWithObjects:wordImage1, wordImage2, wordImage3, wordImage4, wordImage5, wordImage6, wordImage7, wordImage8, wordImage9, wordImage10, wordImage11, wordImage12, nil];
}
这是在加载一组 12 个图像和第二组 12 个图像之间拍摄的 1 个 heapshot 的示例。
Snapshot Timestamp Heap Growth # Persistent
Heapshot 3 00:39.457.174 53.02 KB 800
< non-object > 26.05 KB 277
UIImageView 3.38 KB 36
CFDictionary (mutable) 3.38 KB 72
CFBasicHash (key-store) 2.83 KB 73
CFBasicHash (value-store) 2.83 KB 73
NSPathStore2 2.25 KB 12
CGImageReadRef 1.88 KB 12
CALayer 1.69 KB 36
CGImage 1.62 KB 13
CFNumber 1.31 KB 84
CGImagePlus 1.31 KB 12
CFData 1.12 KB 24
CGImageProvider 768 Bytes 12
CGDataProvider 720 Bytes 5
UIImage 576 Bytes 12
CFString (immutable) 416 Bytes 13
CFArray (mutable-variable) 384 Bytes 12
CGImageReadSessionRef 192 Bytes 12
_ UIImageViewExtendedStorage 192 Bytes 4
__NSArrayM 160 Bytes 5
CFDictionary (immutable) 48 Bytes 1
编辑:
我修改了代码并使数组成为 ivar。我在 Instruments 中获取了另一个分配样本。下面是我的 heapshots 的更详细的显示。每次我用 12 个新图像重置我的阵列时,我都会大吃一惊。每个 heapshot 都有大约 35kb 的 heapgrowth。
Snapshot Timestamp Heap Growth # Persistent
Heapshot 4 00:58.581.296 35.63 KB 680
< non-object > 13.02 KB 220
CFDictionary (mutable) 3.38 KB 72
CFBasicHash (key-store) 2.81 KB 72
CFBasicHash (value-store) 2.81 KB 72
NSPathStore2 2.28 KB 12
CGImageReadRef 1.88 KB 12
CGImage 1.50 KB 12
CFNumber 1.31 KB 84
CGImagePlus 1.31 KB 12
CFData 1.12 KB 24
UIImageView 1.12 KB 12
CGImageProvider 768 Bytes 12
UIImage 576 Bytes 12
CALayer 576 Bytes 12
CFString (immutable) 384 Bytes 12
CFArray (mutable-variable) 384 Bytes 12
CGImageReadSessionRef 192 Bytes 12
CGDataProvider 144 Bytes 1
_UIImageViewExtendedStorage96 Bytes 2
__NSArrayM 32 Bytes 1
这是 UIImage 中这些 Persistent 项之一的堆栈跟踪。它并不指向创建它的特定代码行。不知道从这里去哪里?
24 FourGameCenter 0x4b4bf
23 FourGameCenter 0x4b538
22 UIKit UIApplicationMain
21 GraphicsServices GSEventRunModal
20 CoreFoundation CFRunLoopRunInMode
19 CoreFoundation CFRunLoopRunSpecific
18 CoreFoundation __CFRunLoopRun
17 CoreFoundation __CFRunLoopDoSource1
16 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
15 GraphicsServices PurpleEventCallback
14 GraphicsServices _PurpleEventCallback
13 UIKit _UIApplicationHandleEvent
12 UIKit -[UIApplication sendEvent:]
11 UIKit -[UIWindow _sendTouchesForEvent:]
10 UIKit -[UIControl touchesEnded:withEvent:]
9 UIKit -[UIControl(Internal) _sendActionsForEvents:withEvent:]
8 UIKit -[UIControl sendAction:to:forEvent:]
7 UIKit -[UIApplication sendAction:toTarget:fromSender:forEvent:]
6 UIKit -[UIApplication sendAction:to:from:forEvent:]
5 FourGameCenter 0x8d7fa
4 FourGameCenter 0x71830
3 FourGameCenter 0x797e6
2 libobjc.A.dylib _objc_rootAllocWithZone
1 libobjc.A.dylib class_createInstance
0 libsystem_c.dylib calloc