I have been writing an app at work for some time now that shows an Exchange Calendar’s events for specified conference rooms. I use a handful of NSDateFormatters to pull this off. The app refreshes the onscreen events every 10 minutes, and when it does so, it calls a few functions that calculate various times, like the beginning and end of the week and converting NSDate objects to custom formatted strings. When I was originally writing the code, I was just learning how to use the NSDateFormatter class, and I was instantiating a new date formatter every time the functions were called. Before you experienced programmers laugh at me, I should say that I’m still learning, plus this project uses ARC, so I wasn’t able to just throw a release statement in there like I did in pre-ARC days. That said, I wouldn’t go back to pre-ARC days for anything.

Since I implemented my date formatters incorrectly, the number of data formatters in my app increased steadily until the iPad ran out of memory and crashed. At this point I ran my app through Instruments and found what you see in this screenshot:

Screen-Shot-2012-02-15-at-1.30.30-PM

As you can see in the Responsible Frame column, there are a ton of -[NSDateFormatter _regenerateFormatter] messages. This was a simple fix. I defined an NSDateFormatter variable in my header and then instantiated it in my viewDidLoad method. Then when I need to use it, I just call setDateFormat on it to make sure the format is correct. The result: no leaks! At least no regenerateFormatter leaks:

Screen-Shot-2012-02-15-at-1.30.34-PM

Yes, I have other leaks, but I’ll tackle those later. The goal is obviously 100% leak-free code, but as far as memory consumption goes, I eliminated probably more than 90% of the memory leaks. I hope this helps someone, since my Google search for regenerateFormatter didn’t yield much useful information.