Game Development Community

iPhone app memory problems

by John F · in iTorque 2D · 01/16/2009 (10:13 pm) · 7 replies

My iPhone game sometimes terminates due to memory exhaustion. It is not clear to me why this is the case given that I am allocating all game objects during game initialization and never knowingly delete or create anything afterward. Even so, sometimes the game plays for a long period with no problem and other times it will terminate several times spaced close together. If it is not a leak then perhaps I'm just using too much memory overall. However if this were the case I would expect more consistent behavior. I need to understand iPhone memory allocation better and how I might address the problem in iTGB. If anyone can help me with these questions it would be much appreciated.

1) My game uses a static sprite which I clone 81 times. Does each clone have its own copy of the PNG or do they share a single image in memory? I also use a single image to paint each tile in a tile map. Does each tile in the map have its own copy of the image in memory or do they share a single copy?

2) I use large images in some of my GUIs. How can I ensure that these are not loading until the GUI is made visible and and that they are unload afterward?

3) On the iPhone is the running application sharing memory with any other apps? Does termination of an application free any leaked memory?

4) For a given iPhone device will the application always have the same amount of memory available on launch?

5) Is there a way to ask iTGB how much memory it has allocated?

Any other tips about memory usage, allocation or leaks on the iPhone would be terrific.

Thanks,
John

#1
01/17/2009 (3:17 pm)
1) Each image should only be loaded into memory once

2) I think that if the image is not in a datablock, and only set in you gui declaration i.e. bitmap = "image.png"), it won't get loaded until the GUI is created

3) The iPhone only runs one app at a time*, and I don't think iTGB has much of a problem with leaked memory (I've seen a little, but nothing too significant).

4) I would think that, given 2, you've always got the full amount of system memory (minus whatever the system itself uses)

5) I don't think so, not on iPhone anyway. TGB has an implementation of a memory manager (search the code for TORQUE_DISABLE_MEMORY_MANAGER) that doesn't cooperate with the iPhone too well, but you can enable it on PC/MAC and it will echo() whenever it allocates a new memory page.



You'll probably be wanting to run your game using XCodes "Leaks" instrument. Run it on the device with a release build for the most accurate gauge of what memory your app is actually using.



*I heard apple was going to allow for some kind of background deal, where certain events could start an app, like IM/eMail alerts, but I don't it runs the app in the background, just starts it up, and I'm not sure if that functionality is in or was just planned or what. Even if it is, I would imagine not many apps would use it
#2
01/19/2009 (2:25 am)
4. thats the theory. Reality is that this is not the case. always check the memory you have available, it can be anything from 20-~50mb

5. instruments and the rest of the xcode debugging tools are the way to go. iTGB can only guess its ram usage.
getting used to them also is recommend if you intend to prevent yourself from creating massive leaks which enforce a device restart
#3
06/11/2009 (11:12 pm)
Hi, we have similar problem, our game are running without problem, but time to time just crash down during playing level, this I found in my console, when I try last time with iPhone connected to xcodes:

Fri Jun 12 07:57:39 unknown SpringBoard[22] <Warning>: Memory level is urgent (10%). Asking 'PuzzleRocks' to quit.
Fri Jun 12 07:57:39 unknown SpringBoard[22] <Warning>: Writing low memory report regarding 'PuzzleRocks'
Fri Jun 12 07:57:41 unknown ReportCrash[1269] <Error>: Saved crashreport to /Library/Logs/CrashReporter/PuzzleRocks-2009-06-12-075740.plist using uid: 0 gid: 0, synthetic_euid: 0 egid: 0
Fri Jun 12 07:57:41 unknown SpringBoard[22] <Warning>: Memory level is urgent (10%). Asking 'PuzzleRocks' to quit.
Fri Jun 12 07:57:41 unknown SpringBoard[22] <Warning>: Writing low memory report regarding 'PuzzleRocks'
Fri Jun 12 07:57:42 unknown com.apple.launchd[1] <Notice>: Bug: launchd_core_logic.c:2190 (23720):10: wait4(j->p, &status, 0, &ru) != -1
Fri Jun 12 07:57:42 unknown com.apple.launchd[1] <Notice>: Working around 5020256. Assuming the job crashed.
Fri Jun 12 07:57:42 unknown com.apple.launchd[1] <Warning>: Exited abnormally: Segmentation fault
Fri Jun 12 07:57:50 unknown ReportCrash[1270] <Error>: Saved crashreport to /Library/Logs/CrashReporter/PuzzleRocks-2009-06-12-075750.plist using uid: 0 gid: 0, synthetic_euid: 0 egid: 0
Fri Jun 12 07:57:50 unknown SpringBoard[22] <Warning>: Application <SBApplication: 0x45774e0> cz.cinemax.PuzzleRocks activate:  deactivate: underLock  exited abnormally with signal 11: Segmentation fault

and there is crash log:
Incident Identifier: 08167211-8564-4065-AB63-E9D7B68805C9
CrashReporter Key:   34e621f12eb659941a84f8f8311b28683493284a
OS Version:          iPhone OS 2.2.1 (5H11)
Date:                2009-06-12 07:57:40 +0200

4718592 bytes free
39800832 bytes wired
0 bytes purgeable
Memory status: 10
About to jettison: PuzzleRocks

Processes
  PID  RPRVT  RSHRD  RSIZE Command
    1   252K   248K   368K launchd
   13  72.0K   144K  96.0K update
   15   828K   432K  1.15M lockdownd
   16  1.22M   516K  1.72M mediaserverd
   17   240K   252K   480K mDNSResponder
   19   600K   424K  1.29M iapd
   20   224K   148K   332K fairplayd
   21   612K   356K  1.07M configd
   22  6.52M  7.22M  9.57M SpringBoard
   25   828K   368K  1.28M CommCenter
   26   600K   392K   888K BTServer
   28   244K   168K   296K notifyd
  231   256K   156K   388K SCHelper
  232   412K   188K   488K syslogd
  233   840K   428K  1.36M aosnotifyd
1183   488K   404K  1.25M ptpd
1208   144K   152K   676K afcd
1212   128K   144K   620K syslog_relay
1214   136K   144K   628K notification_pro
1218   136K   144K   628K notification_pro
1238   248K   164K   920K debugserver
1239  34.3M  18.0M  48.3M PuzzleRocks
1268   164K   148K   688K securityd
1269   284K   220K  1.10M ReportCrash

**End**

any idea how to aware this situation? for example how to setup less memory for iTGB?
#4
06/11/2009 (11:17 pm)
or is there chance to hook on "Asking 'PuzzleRocks' to quit." to have chance to save progress before application is killed by OS?
#5
06/12/2009 (2:46 am)
No
You have to store it immediately when you get the quit request or it will be lost
#6
06/12/2009 (12:35 pm)
My frustrations are building up, as my game never lasts for more than 10 minutes before the frame rate drops dramatically and eventually crashes.

At every level, I used to reload the level, and that made it crash faster. Now, I simply add all the new objects to a SimGroup, then delete the SimGroup and reset everything, but it still eventually dies.

Is there something in 1.2.1 that will fix this, has anyone tried the TORQUE_DISABLE_MEMORY_MANAGER feature to see if it makes things worse or better?
#7
06/12/2009 (12:44 pm)
Have you run the Leaks Instrument to see where you are losing memory.

Also, TORQUE_DISABLE_MEMORY_MANAGER should be set, definitely turn that on.