TKP
TKP

Reputation: 453

iOS Memory usage keep increasing

I'm new to iOS development.

I'm building a simple bus schedule app that makes a HTTP request, parse XML, and display/save data. I have four table view controllers and two view controllers. When I start the app, Xcode memory report tells me that its memory usage is ~18MB, which is weird since it's just a simple app. I've looked at similar apps on the app store but they were all less than 10MB. Since it's a bus schedule app, I have it make new requests every 30 seconds to update the time. I noticed that each time I change view to look at another schedule, the memory usage increases and it may go up to 50MB or more.

I looked at Xcode's instrument and it looked like there was no leaks. I also looked at allocations and it looked like live bytes kept increasing every time I switched views.

enter image description here

I am kinda stuck from what to do from here. Can anyone guide me please? Thanks in advance.

Upvotes: 1

Views: 4521

Answers (3)

drekka
drekka

Reputation: 21883

First, take a look at heap shots as @Zaph suggested. Well worth your while in terms of seeing what's being allocated and not released.

Another technique I use is to use instruments filtering options to filter the display for just the classes in my app. Then, I look for classes which have unusual number's of living instances. For example, most view controllers usually have only one active instance. If there are more, then something is retaining them. Blocks and notification listeners are often the cause.

In your example screen shot, I noticed two things that I would investigate.

Firstly, you have 365 living core animations taking some 30M. Not sure why, but worth thinking about.

Secondly, near the bottom, I noticed 166 table view cell scroll views. Sounds like a lot. I can't see how many living table view controllers you have, but I'm wondering if they are not reallocing. Filtering this list will help establish what's not being dealloc'd.

Finally, leaks can be useful in finding issues, but most of the time what we would regard as a leak looks ok to instruments. So, looking at memory and numbers of allocated instances is a far better guide to finding where things are not being dealloc'd.

Upvotes: 2

zaph
zaph

Reputation: 112857

Use Heapshot to find memory creep. (see: bbum blog)

Basically, their method is to run the Instruments allocate tool, take a heapshot, run an iteration of your code and another heapshot, repeating 3 or 4 times. This will indicate memory that is allocated and not released during the iterations.

In this case, take a heapshot (called something different now) between downloads.

To figure out the results, disclose to see the individual allocations.

If you need to see where retains, releases, and autoreleases occur for an object use Instruments:

Run in Instruments, in Allocations set "Record reference counts" to on (you will have to stop recording to set the option). Allow the problem code to run, stop recording, then search for the object of interest. After that, you'll need to drill down and you will be able to see where all retains, releases and autoreleases occurred.

enter image description here

Upvotes: 2

user3074773
user3074773

Reputation: 121

Hard to say without any code, but it looks like you have a retain cycle somewhere.

Try to add a dealloc method to the view controllers and make sure that you enter it whenever the view controller disappears.

You say that you make a new request every 30 seconds. If you use a timer with the view controller as a target, the timer has a strong reference to the view controller and vice versa. You have to invalidate the timer when your view controller disappears.

Upvotes: 1

Related Questions