22

I am running my iOS App on iPod touch device and I get memory warnings even if the total allocation peak is only 7 MB as shown below (this happens when the Game Scene is pushed):

low memory warning

What I find strange is that:

  • the left peak (at time 0.00) corresponds to 20 MB of memory allocated (Introduction Scene) and despite this DOES NOT give any memory warning.

  • the central peak (at time 35.00) corresponds to raughly 7 MB of memory allocated (Game Scene is being pushed) and DOES give memory warning.

I do not understand why I get those warnings if the total memory is only 7 MB. Is this normal? How can I avoid this?

Density peaks

Looking at the allocation density we can see the following schema, which (to me) does not show much difference between the moment when the Intro Scene is being pushed (0.00) and the moment in which the Game Scene is being pushed (35.00). Being the density peaks similar I would assume that the memory warnings are due to something else that I am not able to spot.

EDIT:

I have been following a suggestion to use "Activity monitor" instead but unfortunately my App crashes when loading the Game Scene with only 30 MB of memory allocated. Here is the Activity monitor report.

Activity monitor report

Looking at the report I can see a total real memory usage sum of about 105 MB. Given this should refer to RAM memory and given my model should have 256 MB of RAM this should not cause APP crashes or Memory leaks problems.

I run the Leak monitor and it does not show any leak on my App. I also killed all the other apps.

However, analyzing the report, I see an astonishing 167 MB of Virtual Memory value associated to my App. Is this normal? What does that value mean? Can this be the reason for the crash? How can I detect which areas of my code are responsible for this?

Virtual memory

My iPod is a 4th Generation model with 6.4 GB of capacity (memory) and only 290 MB of memory free. I am not sure if this somehow effects the Virtual Memory paging performance.

EDIT 2: I have also looked more at SpringBoard and its Virtual Memory usage is 180 MB. Is this normal? I found some questions/answers that seem to suggest that SpringBoard is responsible for autoreleasing objects (it should be the process for managing the screen and home botton but I am not sure if it has also to do with memory management). Is this correct?

Another note. I am using ARC. However I am not sure this has to do much with the issue as there are no apparent memory leaks and XCode should convert the code adding release/dealloc/retain calls to the compiled binary.

EDIT 3: As said before I am using ARC and Cocos2d (2.0). I have been playing around with the Activity monitor. I found out that if I remove the GameCenter authentication mechanism then the Activity Monitor runs fine (new doubt: did anyone else had a similar issue? Is the GameCenter authentication view being retained somewhere?). However I noticed that every time I navigate back and forwards among the various scenes prior the GameScene (Initial Scene -> Character Selection -> Planet Selection -> Character Selection -> Planet Selection -> etc.. -> Character Selection ..) the REAL MEMORY usage increases. After a while I start to get memory warnings and the App gets killed by iOS. Now the question is:

-> am I replacing the scenes in the correct way? I call the following from the various scene:

[[CCDirector sharedDirector] replaceScene: [MainMenuScene scene]];

I have Cocos2d 2.0 as static library and the code of replaceScene is this:

-(void) replaceScene: (CCScene*) scene
{
    NSAssert( scene != nil, @"Argument must be non-nil");

    NSUInteger index = [scenesStack_ count];

    sendCleanupToScene_ = YES;
    [scenesStack_ replaceObjectAtIndex:index-1 withObject:scene];
    nextScene_ = scene; // nextScene_ is a weak ref
}

I wonder if somehow the scene does not get deallocated properly. I verified that the cleanup method is being called however I also added a CCLOG call on the CCLayer dealloc method and rebuild the static library. The result is that the dealloc method doesn't seem to be called.

Is this normal? :D

I found that other people had similar issues. I am wondering if it has to do with retain cycles and self blocks. I really need to spend some time studying this unless, from EDIT 3, anyone can tell me already what I am doing wrong :-)

Community
  • 1
  • 1
mm24
  • 9,280
  • 12
  • 75
  • 170
  • 1
    Any other apps running in the background? – Fahri Azimov Sep 03 '13 at 10:43
  • 5
    Graphics memory, e.g. for large images or textures will not appear in the Allocations instrument. Use Activity Monitor instead. – Mike Weller Sep 03 '13 at 10:46
  • try to push and come back to view again few times. Check when you try to push and pop the view ,is there exactly you get memory warnings? – Manthan Sep 03 '13 at 11:15
  • which iPod Touch model u are working on? – Saurabh Passolia Sep 03 '13 at 11:17
  • @samfisher I am using iPod Touch 4th Generation – mm24 Sep 03 '13 at 11:45
  • @MikeWeller I have just added a new Activity monitor analysis. Unfortunately my App crashes when pushing the game Scene now (it used to work fine but using this Activity monitor seems to affect the App even more and makes it crashing). – mm24 Sep 03 '13 at 12:05
  • 1
    @mm24 choose "All processes" for the activity monitor. This shouldn't cause your app to crash. Also, use the opengl instruments if you are doing opengl stuff. – Mike Weller Sep 04 '13 at 07:05
  • @MikeWeller thanks I did. I noticed that the App crashes using Activity Monitor and having GameCenter enabled. I guess that that "extra view" that says "Player authenticated" is causing somehow issues. However, as described in the new EDIT 3, I found out that when parsing between scens the previous scene memory keeps growing similarly to what described here http://www.cocos2d-iphone.org/forums/topic/cclayer-vs-ccscene-somewhat-confused/ . I am looking into ARC and retain counts to solve the issue, however I am confused by the fact that the static library does not use ARC but my game does. – mm24 Sep 04 '13 at 09:25
  • Also worth noting that the Allocations tool in Xcode 5's Instruments will now show all this stuff by default. – Mike Weller Sep 04 '13 at 10:29
  • I am using XCode 4.. :) but thanks for pointing this out :) – mm24 Sep 04 '13 at 13:32

2 Answers2

3

All memory capacity shared through all apps&processes run in iOS. So, other apps can use a lot of memory and your app receive memory warning too. You'll receive memory warnings until it is not enough.

To understand what actually happens with memory in your app you should

  1. Profile your app with Leaks (ARC is not guarantee that you don't have leaks, i.e. self-capturing issue).

  2. Use heapshot analysis (shortly described here http://bentrengrove.com/blog/2013/4/26/heapshot-analysis)

And checkout this post about memory & virtual memory in iOS: http://liam.flookes.com/wp/2012/05/03/finding-ios-memory/

otmezger
  • 10,410
  • 21
  • 64
  • 90
Ilia
  • 1,434
  • 15
  • 22
  • Thanks for your answer. It does definetly help me to think about ARC and self capturing issue. I am investigating the matter and added some more insights on my issue in the question (see EDIT 3). Still not solved so any further help from anyone would be greatly apprecaited. – mm24 Sep 04 '13 at 09:22
0

I solved this by adding a print of the process effective memory usage in the console. In this way I could get a precise measurament of the real memory used by the App process. Using instrument proved to be imprecise as the real memory used did not match with the one shown on instruments.

This code can be used to get the effective memory usage:

-(vm_size_t)report_memory
{    
    struct task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                                   TASK_BASIC_INFO,
                                   (task_info_t)&info,
                                   &size);
    if( kerr == KERN_SUCCESS ) {
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
    }
    return info.resident_size;
}
mm24
  • 9,280
  • 12
  • 75
  • 170
  • 1
    Well, if you have extra information/help about this, i'm facing the exact same problem :( – Gil Sand Sep 26 '14 at 08:13
  • @Zil sorry didn't get notified of the comment.. I will look back at my source code (Is not on my computer now) and retrieve the code I have used to print it. – mm24 Dec 13 '14 at 13:30
  • 6
    And the answer was...? – Chris Apr 28 '15 at 05:45
  • You need to add some example. Like go here to see how to print memory usage to console. http://stackoverflow.com/questions/7989864/watching-memory-usage-in-ios – ucangetit Aug 13 '15 at 22:33