Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get memory warnings with only 7 MB of memory allocated?

Tags:

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 :-)

like image 594
mm24 Avatar asked Sep 03 '13 10:09

mm24


People also ask

What is physical memory usage?

Physical Memory: The amount of RAM installed. Memory Used: The amount of RAM being used. To the right, you can see where the memory is allocated. App Memory: The amount of memory being used by apps. Wired Memory: Memory required by the system to operate.

What is memory usage?

The MEMORY USAGE command reports the number of bytes that a key and its value require to be stored in RAM. The reported usage is the total of memory allocations for data and administrative overheads that a key its value require.


1 Answers

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/

like image 87
Ilia Avatar answered Nov 17 '22 22:11

Ilia