0

I'm using ARC and NSCache which is created and stored on my app delegate. Then I call it from the controllers trough the app delegate. The NSCache stores images as they are loaded from an url and the memory usage goes up really quick. When I check the profiler for real memory usage, my app reaches even 320 MB of memory usage but on allocations it says it has just allocated 20-30 MB.

On my app delegate I set the cache as follows (it is an ivar):

cache = [[NSCache alloc] init];
[cache setCountLimit:100];
[cache setTotalCostLimit:1500000];
[cache setEvictsObjectsWithDiscardedContent:YES];

I implemented a button to experiment with NSCache and when I click on it it calls:

- (IBAction)eraseCache:(id)sender {
    [[appDelegate cache] removeAllObjects];
}

On the profiler, the memory used does not go down, but it actually starts to get the images again, so I know the objects where removed. How can I release this memory usage at will using ARC? How can I get the size of my cache to know when to release it?

clopez
  • 4,372
  • 3
  • 28
  • 42

1 Answers1

0

In ARC, once there are no pointers to an object, it's automatically released. If the only pointers you had to the object were in the cache, then they have been released.

Note that you don't actually have to remove the objects; if you assign the pointer to a new object (with the result that it no longer points at the old object) then the old object is deallocated.

Ex:
NSArray *array = [NSArray new];
array = [NSArray new]; //the original array gets deallocated because nothing points to it.

From the NSCache Class Reference:

The NSCache class incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. The system automatically carries out these policies if memory is needed by other applications. When invoked, these policies remove some items from the cache, minimizing its memory footprint.

Dustin
  • 6,783
  • 4
  • 36
  • 53
  • Then, why te memory usage is not going down when I press my "removeAllObjects" button? What could be happening? – clopez Jul 27 '12 at 16:28
  • It might just stay in memory until the OS decides it wants that memory again. Did your app crash? – Dustin Jul 27 '12 at 16:29
  • Do you have any other references to the pictures? Note that if they are put into a view this counts as a strong reference. Also make sure that your content is set as discardable. – Dustin Jul 27 '12 at 16:31
  • 1
    Yes, they are in a UIImageView and those are shown on a ViewController. What do you recommend to do? By the way, what do you mean by "set as discardable". – clopez Jul 27 '12 at 16:40
  • Read the NSCache class reference, you can tell it whether to discard an image. If you are showing a lot of images, then remove the image view from the view controller when you need to free up memory. – Dustin Jul 27 '12 at 16:48
  • @clopez I think he means to implement the NSDiscardableContent protocol on your cached objects. http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSDiscardableContent_Protocol/Reference/Reference.html Unfortunately I am having to store UIImage objects, and I can't take this approach – Sunil Gowda Dec 06 '12 at 04:09