3

My app(arc, ios5) sometimes will crash after I take a photo and save to CoreData. Using Instruments I found that there are memory warnings before crash, so I tried to find out how memory is used.

My app stores trips in CoreData (totally 200MB), each trip has hundreds of CLLocation objects and tens of UIImage objects.

Each time I view one trip details by pushViewController, the memory usage grows up; but after I popViewController, the memory usage remains.

I think there are several suspects:

  1. the CoreData cache, but I don't know how to purge it;
  2. the detail view controllers, since I didn't see any viewDidUnload called after each popViewController;

After some tests, I was confused that even if I call viewDidUnload manually, the memory usage remains.

I also tested memory leaks instrument in ios5.0 simulator for my app, it's clean.

iDev
  • 23,310
  • 7
  • 60
  • 85
DoZerg
  • 43
  • 5

2 Answers2

1

It is not advised to store images in coredata due to memory issues. Check if you can store it in document folder or so and store the image name/path in your coredata. Most of the cases that should resolve memory issues.

Here are a few posts about this CoreData (for iphone) storing images and Core Data - Storing Images (iPhone)

Community
  • 1
  • 1
iDev
  • 23,310
  • 7
  • 60
  • 85
  • Thanks for your reply. I tested to 'nil' all images, and the memory usage grows slower as expected. But my question remains: how can I release the unused memory, so the app won't get memory warnings after long time running? Thanks again! – DoZerg Oct 11 '12 at 06:55
  • You may have to manually handle that. For example when you are moving to a new view on tap of a button, you can code for releasing unwanted objects used in the previous view. And whenever user comes back load the things again. If it is an image, save it to disc and load it again when user tries to comeback. That is one way to handle these issues. – iDev Oct 11 '12 at 07:00
  • Thanks, I'm now modifying the CoreData to store images out to file system. I hope it will work. – DoZerg Oct 12 '12 at 04:34
  • 1
    well, I've solved the problem by move all images to file system, and use *NSCache* to cache image objects. Now my app use memory limited in 60 MB, and won't crash for memory warnings. Thanks a lot! – DoZerg Oct 12 '12 at 10:23
1

First thing, do not use core data to store the image data. On iOS 5 and above, you can check a property to "Allow External Storage" on the file inspector of the property. You can also save the images on the document directories and use core data to save the path to the images. When presenting these images to the interface, cache them on an NSCache, so you can purge these images when memory warning start popping up. Changing the approach you manage these images will have a significant impact on your memory response during low memory warnings.

J2theC
  • 4,412
  • 1
  • 12
  • 14