From 502f74042f10aa3a34d95a5e3f3e9a6500471554 Mon Sep 17 00:00:00 2001 From: Jesse Andersen Date: Tue, 5 Jun 2012 22:15:04 -0700 Subject: [PATCH] Use NSCache instead of a dictionary for the memory cache. --- SDWebImage/SDImageCache.h | 12 +------- SDWebImage/SDImageCache.m | 60 ++------------------------------------- 2 files changed, 4 insertions(+), 68 deletions(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index c21ca8b..a035c6a 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -15,7 +15,7 @@ */ @interface SDImageCache : NSObject { - NSMutableDictionary *memCache; + NSCache *memCache; NSString *diskCachePath; NSOperationQueue *cacheInQueue, *cacheOutQueue; } @@ -127,14 +127,4 @@ */ - (int)getDiskCount; -/** - * Get the total size of images in memory cache - */ -- (int)getMemorySize; - -/** - * Get the number of images in the memory cache - */ -- (int)getMemoryCount; - @end diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 6c17bee..761dcbd 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -16,31 +16,6 @@ static SDImageCache *instance; static NSInteger cacheMaxCacheAge = 60*60*24*7; // 1 week -static natural_t minFreeMemLeft = 1024*1024*12; // reserve 12MB RAM - -// inspired by http://stackoverflow.com/questions/5012886/knowing-available-ram-on-an-ios-device -static natural_t get_free_memory(void) -{ - mach_port_t host_port; - mach_msg_type_number_t host_size; - vm_size_t pagesize; - - host_port = mach_host_self(); - host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t); - host_page_size(host_port, &pagesize); - - vm_statistics_data_t vm_stat; - - if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) - { - NSLog(@"Failed to fetch vm statistics"); - return 0; - } - - /* Stats in bytes */ - natural_t mem_free = vm_stat.free_count * pagesize; - return mem_free; -} @implementation SDImageCache @@ -51,7 +26,8 @@ static natural_t get_free_memory(void) if ((self = [super init])) { // Init the memory cache - memCache = [[NSMutableDictionary alloc] init]; + memCache = [[NSCache alloc] init]; + memCache.name = @"ImageCache"; // Init the disk cache NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); @@ -177,11 +153,7 @@ static natural_t get_free_memory(void) UIImage *image = [arguments objectForKey:@"image"]; if (image) - { - if (get_free_memory() < minFreeMemLeft) - { - [memCache removeAllObjects]; - } + { [memCache setObject:image forKey:key]; if ([delegate respondsToSelector:@selector(imageCache:didFindImage:forKey:userInfo:)]) @@ -228,10 +200,6 @@ static natural_t get_free_memory(void) return; } - if (get_free_memory() < minFreeMemLeft) - { - [memCache removeAllObjects]; - } [memCache setObject:image forKey:key]; if (toDisk) @@ -283,10 +251,6 @@ static natural_t get_free_memory(void) image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]); if (image) { - if (get_free_memory() < minFreeMemLeft) - { - [memCache removeAllObjects]; - } [memCache setObject:image forKey:key]; } } @@ -411,22 +375,4 @@ static natural_t get_free_memory(void) return count; } -- (int)getMemorySize -{ - int size = 0; - - for(id key in [memCache allKeys]) - { - UIImage *img = [memCache valueForKey:key]; - size += [UIImageJPEGRepresentation(img, 0) length]; - }; - - return size; -} - -- (int)getMemoryCount -{ - return [[memCache allKeys] count]; -} - @end