I'm having problem in virtual memory, where the application loads all images within an array (using the [UIImage initWithContentOfFile: path]) and then I press a button, and these images are exchanged, but the virtual memory is increased to the point as the application closes because of space. I don't make allocation, only recover image of the array.
Questions ...
Because virtu开发者_C百科al memory is never released and will increasing more and more? What do I release the virtual memory?
Could anyone help me?
Thanks in advance!
The iPhone does not use virtual memory at all. You need to release the old image, and display the new one so the resources are released. Keep in mind, the rules however. If you are not the only caller who owns the object, the memory won't go away when you release it. So if you have this image in an array or another collection for example, where it implicitly retains the objects added to it, you will have to remove it from that beforehand.
What you may be able to do however, is I'm assuming these are rather large images. Let's just say, 2000x2000 full colour, for the sake of simplicity. That means those images will use 16 MB of memory each. That is just the raw contents.
Since the phone's resolution is at max (pixel count) 960x640, you can, scale your image down, maintaining aspect ratio, so that width and height never go below the upper bounds on the 960 or 640 size, so your image would be 1333x1333 scaled down, which now reduces your memory footprint from 16 MB per image, down to just over 7.1 MB per image. You can now, after scaling, load twice as many images as you could before, which may be all that you need, to allow time for the memory resources to be reclaimed from the other image you're unloading.
why not use [UIImage imageWithContentsOfFile:path]? you will still need to be careful how many you load, but they will be autoreleased, so only the ones currently in the array/retained will hang around.
精彩评论