简体   繁体   English

仪器(Leaks)和NSDateFormatter

[英]Instruments (Leaks) and NSDateFormatter

When I run my iPhone app with Instruments Leaks and parse a bunch of NSDates using NSDateFormatter my memory goes up about 1mb and stays even though these NSDates should be dealloc'd after the parsing (I just discard them if they aren't new). 当我使用Instruments Leaks运行我的iPhone应用程序并使用NSDateFormatter解析一堆NSDate时,我的内存大约增加1mb并保持不变,即使这些NSDate在解析后应该被释放(如果它们不是新的,我只是丢弃它们)。

I thought the malloc (in my heaviest stack trace below) could become part of the NSDate but I also thought it could be memory that only used during some intermediate step in parsing. 我认为malloc(在我下面最重的堆栈跟踪中)可以成为NSDate的一部分,但我也认为它可能是仅在解析的某个中间步骤中使用的内存。 Does anyone know which one it is or how to find out? 有谁知道它是哪一个或如何找出?

Also, is there a way to put a breakpoint on NSDate dealloc to see if that memory is really being reclaimed? 另外,有没有办法在NSDate dealloc上放置一个断点来查看该内存是否真的被回收了?

Here's what my date formatter looks like for parsing these dates: 这是我的日期格式化程序在解析这些日期时的样子:

df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"EEE, d MMM yyyy H:m:s z"];

Here's the Heaviest Stack trace when the memory bumps up and stays there: 当内存碰到并停留在那里时,这是最重的堆栈跟踪:

   0 libSystem.B.dylib  208.80 Kb     malloc
   1 libicucore.A.dylib  868.19 Kb     icu::ZoneMeta::getSingleCountry(icu::UnicodeString const&, icu::UnicodeString&)
   2 libicucore.A.dylib  868.66 Kb     icu::ZoneMeta::getSingleCountry(icu::UnicodeString const&, icu::UnicodeString&)
   3 libicucore.A.dylib  868.67 Kb     icu::ZoneMeta::getSingleCountry(icu::UnicodeString const&, icu::UnicodeString&)
   4 libicucore.A.dylib  868.67 Kb     icu::DateFormatSymbols::initZoneStringFormat()
   5 libicucore.A.dylib  868.67 Kb     icu::DateFormatSymbols::getZoneStringFormat() const
   6 libicucore.A.dylib  868.67 Kb     icu::SimpleDateFormat::subParse(icu::UnicodeString const&, int&, unsigned short, int, signed char, signed char, signed char*, icu::Calendar&) const
   7 libicucore.A.dylib  868.67 Kb     icu::SimpleDateFormat::parse(icu::UnicodeString const&, icu::Calendar&, icu::ParsePosition&) const
   8 libicucore.A.dylib  868.67 Kb     icu::DateFormat::parse(icu::UnicodeString const&, icu::ParsePosition&) const
   9 libicucore.A.dylib  868.67 Kb     udat_parse
  10 CoreFoundation  868.67 Kb     CFDateFormatterGetAbsoluteTimeFromString
  11 CoreFoundation  868.67 Kb     CFDateFormatterCreateDateFromString
  12 Foundation  868.67 Kb     -[NSDateFormatter getObjectValue:forString:range:error:]
  13 Foundation  868.75 Kb     -[NSDateFormatter getObjectValue:forString:errorDescription:]
  14 Foundation  868.75 Kb     -[NSDateFormatter dateFromString:]

Thanks! 谢谢!

[df setDateFormat:@"EEE, d MMM yyyy H:m:s z"]; // Remove the `z`

868 Kb will be permanently allocated on iPhone OS 2.2.1 or 3.1.2 (more to come) after a single invocation to "dateFromString" when the z option is used. 在使用z选项单次调用“dateFromString”后,将在iPhone OS 2.2.1或3.1.2(更多信息)上永久分配868 Kb。

A complete article with source code and log file can be read at http://thegothicparty.com/dev/article/nsdateformatter-memory-leak/ 有关源代码和日志文件的完整文章可以在http://thegothicparty.com/dev/article/nsdateformatter-memory-leak/上阅读。

There may be a problem with NSDateFormatter parsing date strings with time zones because when I changed the formatter pattern to remove the timezone portion the problem disappeared. NSDateFormatter可能存在使用时区解析日期字符串的问题,因为当我更改格式化程序模式以删除时区部分时,问题就消失了。

I changed it from this: 我改变了这个:

[df setDateFormat:@"EEE, d MMM yyyy H:m:s z"];

To this: 对此:

[df setDateFormat:@"EEE, d MMM yyyy H:m:s"];

But now the problem is the dates don't get the right timezone so I'll have to have to determine the timezone myself. 但现在问题是日期没有得到正确的时区所以我必须自己确定时区。

What do you mean by discarding the NSDateFormatters? 丢弃NSDateFormatters是什么意思? Do you release them when you are done with them? 你完成它们后会释放它们吗?

df = [[NSDateFormatter alloc] init]; // allocates memory

Your code allocates memory but you need to call 您的代码分配内存,但您需要调用

[df release];

when you are done with them. 当你完成它们。 When you allocate (or copy) object its reference count is incremented by one. 分配(或复制)对象时,其引用计数会增加1。 When you release object (you send release message to it) the reference count goes down by one. 当您释放对象(向其发送release消息)时,引用计数减少一个。 When the reference count reaches 0 the object is deallocated. 当引用计数达到0时,对象将被释放。

If you don't send it release message it will stay in memory and you have memory leak. 如果你没有发送它的release消息,它将留在内存中,你有内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM