Caching vs. retaining

Discussion in 'iOS Programming' started by counciler, Mar 6, 2011.

  1. counciler macrumors newbie

    Joined:
    Jan 8, 2011
    #1
    So I'm curious about what exactly caching does. As I understand it, the imageNamed method automatically caches images while imageFromContentsOfFile doesn't . Also, caching stores the images into the memory and is harder to release, but imageFromContentsOfFile puts the images in memory until it's ready to be released. So what's the difference between caching and just simply not releasing it from memory?
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    A cached image only has to be read from flash once, and may not be purged from memory even after all UIImage objects that use it are dealloced.

    A non-cached image has to be ready from flash every time it is used by a new UIImage object, but will be purged from memory when the UIImage objects are dealloced.

    It is usually a better idea to use cached images unless you are under really tight memory conditions that you need to control more precisely.
     
  3. counciler, Mar 7, 2011
    Last edited: Mar 7, 2011

    counciler thread starter macrumors newbie

    Joined:
    Jan 8, 2011
    #3
    So if I were to cache an image there would no longer be a need to keep track of the retain/releases for that object since it's futile to do so?

    Is there any way to completely clear the cache? Like let's say I want to have an app that has multiple games. While they're playing one game I'll cache all the images for that game. But when they exit the game, those images are no longer needed. After booting up several games the memory could start to get bloated. Does the cache get cleared after the app exits? I have heard of springboard crashes.
     
  4. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    You most certainly do still need to use proper memory management for UIImage objects regardless of whether they are using cached images or not. There will be other data besides just the images that you need to clear from memory. It will also make it so you or anyone else looking at your code doesn't have to try to figure out which UIImages are using cached images so that they can figure out which to do memory management on.

    In theory the system should manage the cache so that it doesn't use an excessive amount of memory. If you run into problems, then you can switch to uncached images. The cache will be cleared when the application exits.
     

Share This Page