我创建了一个示例应用程序,它声明了一个宏,如下所示:
#define kSampleString @"didReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarningdidReceiveMemoryWarning"
当用户点击下面的事件处理程序时,我会记录宏字符串。
-(IBAction)SampleMethod:(id)sender{
NSLog(@"Log %@",kSampleString);
}
下面是我得到的仪器数据:
在检查 Instruments 中的分配时,我得到了以下信息。我能够发现分配发生在 NSLogv 调用中标记为红色的区域中。但是如何解释关于 NSLogv 发生的其他事情。这些 malloc 操作发生了什么?
最佳答案
Malloc 只是 objective-c 的 alloc 的 C 版本,最终会在执行 alloc 时调用。 Instruments 正在跟踪您应用程序中的所有分配,因此 Malloc 出现是很自然的。
至于 CFString,它是 NSString 的 CoreFoundation 堂兄弟(因为 NSString 是围绕 CFString 构建的类集群),无论如何你总是应该向 NSLog() 传递一个不可变的字符串,所以这很有意义.如果您的应用程序正在泄漏内存,那将是一个问题,但目前,一切都很好。
我会担心的是巨大的 1.5 KB 字符串 Malloc 大约位于列表的一半。对于一个简单的字符串来说,这似乎占用了太多内存。
关于ios - 如何解释这个 iOS Instruments 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10527053/