Understanding iPhone Memory Consumption

I am working on a 2D iPhone game using OpenGL ES and I continue to beat the 24 MB memory limit - my application continues to crash with error code 101. I tried very hard to find where the memory is, but the numbers in the tools are still much larger than I expected.

I launched the application using the tools Memory Monitor, Object Alloc, Leaks and OpenGL ES. When the application loads, free physical memory drops from 37 MB to 23 MB, the Alloc object takes about 7 MB, Leaks has two or three leaks in several bytes, the size of the Gart object is about 5 MB, and the Memory Monitor application takes about 14 MB real memory. I am puzzled at how memory happens - when I dig into Object Allocations, most of the memory is in textures, just like I would expect. But my own texture distribution counter and the size of the Gart object agree that textures should occupy about 5 MB.

I donโ€™t know how to highlight anything else worth mentioning, and Object Alloc agrees. Where is the memory going? (I would be happy to provide more details if this is not enough.)




Update: I was really trying to find where I could allocate so much memory, but without any results. I am distinguished by the difference between the distribution of objects (~ 7 MB) and the use of real memory, as shown in Monitor Memory (~ 14 MB). Even if there were huge leaks or huge chunks of memory that I forgot about, they should appear in Object Allocations, right?

I already tried the usual suspect , i.e. UIImage with its caching, but this did not help. Is there a way to track the memory usage of the "debugger style", taking turns observing how each statement affects memory usage?




What I have found so far:

  • I use so much memory. It is not easy to measure the consumption of real memory, but after a big calculation, I think that the memory consumption is really large. My mistake.

  • I did not find a simple way to measure the memory used. The memory monitor numbers are accurate (these are the numbers that really matter), but the memory monitor cannot indicate where the memory is located. The Object Alloc tool is almost useless for tracking real memory usage. When I create a texture, the allocated memory counter rises for a while (reading the texture into memory), then drops (transferring texture data to OpenGL, freeing up). This is normal, but this does not always happen - sometimes the memory usage remains high even after the texture has been transferred to OpenGL and freed from "my" memory. This means that the total amount of memory allocated as shown by the Object Alloc tool is less than the actual total memory consumption, but more than the actual consumption minus the textures ( real โ€“ textures < object alloc < real ). Go ahead.

  • I read the Programming Guide incorrectly. The 24 MB memory limit refers to textures and surfaces, not the whole application. The actual red line lies a little further, but I could not find any hard numbers. The consensus is that 25-30 MB is the ceiling.

  • When the system becomes short in memory, it begins to send a warning about the memory. Almost nothing frees me, but other applications free memory back to the system, especially Safari (which seems to cache websites). When the free memory, as shown on the memory monitor, is zero, the system begins to kill.

I had to bite a bullet and rewrite some parts of the code in order to be more efficient in memory, but I probably still click on it. If I had to develop another game, I would probably think of some kind of resource. With the current game, this is quite difficult, because the thing is in motion all the time and loading textures interferes, even if done in another thread. I would be very interested in how other people will solve this problem.

Please note that these are only my views, which do not have to be accurate. If I find out anything else about this, I will update the question. I will keep the question open if someone who understands this question answers, because all of these are more workarounds and conjectures than anything else.

+34
memory-management iphone opengl-es
Dec 12 '08 at 17:22
source share
5 answers

I doubt very much that this is a mistake in the Tools.

First read this Jeff Lamarche blog post about openGL textures :

  • has a simple example of loading textures without causing leaks.
  • gives an idea of โ€‹โ€‹how โ€œsmallโ€ images to get after loading them in openGL, actually use a lot of memory

Excerpts:

Textures, even if they are made from compressed images, use a bunch of application memory because they must be expanded in memory in order to be used. Each pixel takes four bytes, so we forget to release the texture image data can really quickly.

Secondly, you can debug texture memory using tools. There are two profiling configurations: OpenGL ES Analyzer and OpenGL ES Driver . You will need to run them on the device, since the simulator does not use OpenGL. Just select Product-> Profile from Xcode and find these profiles after starting the tools.




Armed with this knowledge, this is what I will do:

  • Make sure you are not leaking memory - this will obviously cause this problem.
  • Make sure you are not accessing autorealized memory - a common cause of crashes.
  • Create a separate test application and play with downloading the textures individually (and in combination) to find out which texture (or a combination of them) is causing the problem.

UPDATE:. After thinking about your question, I read the Apple OpenGL ES Programming Guide and it has very good information. Highly recommended!

+11
Mar 19 '10 at 8:28
source share

One way is to start commenting on the code and see if the error goes away. Yes, this is tedious and elementary, but it can help if you know where the error was.

Where is he failing, why is he failing, etc.

+3
Dec 16 '08 at 1:15
source share

Hrmm, this is not so much detail, but if the leaks do not show where the leaks are, there are two important options:

[i] Leaks missed the leak [ii] Memory does not actually leak.

fixing [i] is quite difficult, but as Eric Albert said, submitting a bug report with Apple will help. [ii] means that the memory you are using is still available somewhere, but you may have forgotten about it. Do some lists grow without throwing old records? Are any realloc () ed buffers a lot?

+2
Dec 13 '08 at 23:41
source share

For those who see this after 2012:

The memory actually loaded into the physical memory of the device is resident memory in the VM tracking tool.

The Distribution Tool only marks the memory created by malloc / [NSObject alloc], and some frame buffer, such as a bitmap with a decompressed image, is not included in the Distribution Tool, but it always takes up most of your memory.

Please see WWDC 2012 Session 242 iOS Application Performance: Memory for information from Apple.

+2
Aug 22 '13 at 1:56 on
source share

This will not help you, but if you find that the memory tools do not provide all the data you need, please report a bug at bugreport.apple.com. Attach a copy of your application and a description of how the tools do not fit your analysis, and Apple will see if they can improve the tools. Thank!

0
Dec 12 '08 at 19:56
source share



All Articles