title: Memory continuously increase then app crash when i display image from document directory during scrolling UITableview
body: <p>My Requirement is download all images in application memory and display it from local if its available.</p>
  
  <p>Below is my code to access image from local and if its not available then it will download then display.</p>
  
  <pre><code>[cell.imgProfilePic processImageDataWithURLString:cData.PICTURE];
  </code></pre>
  
  <p>I have made custom <code>UIImageView</code> class</p>
  
  <p><strong>DImageView.h</strong> </p>
  
  <pre><code>    #import &lt;UIKit/UIKit.h&gt;
      @interface DImageView : UIImageView
      @property (nonatomic, strong) UIActivityIndicatorView *activityView;
      - (void)processImageDataWithURLString:(NSString *)urlString;
      + (UIImage *)getSavedImage :(NSString *)fileName;
      @end
  </code></pre>
  
  <p><strong>DImageView.m</strong> </p>
  
  <pre><code>#import "DImageView.h"
  #define IMAGES_FOLDER_NAME  @"DImages"
  
  @implementation DImageView
  
  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self)
      {    }
      return self;
  }
  - (void)dealloc
  {
      self.activityView = nil;
      [super dealloc];
  }
  - (id)initWithCoder:(NSCoder *)aDecoder
  {
      self = [super initWithCoder:aDecoder];
      if (self)
      {
          [self initWithFrame:[self frame]];
      }
      return self;
  }
  - (void)processImageDataWithURLString:(NSString *)urlString
  {
      @autoreleasepool
      {
      UIImage * saveImg =[DImageView getSavedImage:urlString];
      if (saveImg)
      {
          @autoreleasepool
          {
          dispatch_queue_t callerQueue = dispatch_get_main_queue();
          dispatch_async(callerQueue, ^{
  
              @autoreleasepool{
              [self setImage:saveImg];
              }
          });
      }
      }
      else
      {
          [self showActivityIndicator];
          NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  
          dispatch_queue_t callerQueue = dispatch_get_main_queue();
          dispatch_queue_t downloadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
          __block NSError* error = nil;
          dispatch_async(downloadQueue, ^{
  
              @autoreleasepool
              {
                  NSData * imageData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&amp;error];
                  if (!error)
                  {
                      dispatch_async(callerQueue, ^{
                          @autoreleasepool {
                          UIImage *image = [UIImage imageWithData:imageData];
                          [self setImage:image];
                          [self hideActivityIndicator];
                          [self saveImageWithFolderName:IMAGES_FOLDER_NAME AndFileName:urlString AndImage:imageData];
                          }
                      });
                  }
              }
          });
          dispatch_release(downloadQueue);
      }
      }
  }
  - (void) showActivityIndicator
  {
      self.activityView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
      self.activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
      self.activityView.hidesWhenStopped = TRUE;
      self.activityView.backgroundColor = [UIColor clearColor];
      self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
  
      [self addSubview:self.activityView];
      [self.activityView startAnimating];
  }
  - (void) hideActivityIndicator
  {
      CAAnimation *animation = [NSClassFromString(@"CATransition") animation];
      [animation setValue:@"kCATransitionFade" forKey:@"type"];
      animation.duration = 0.4;;
      [self.layer addAnimation:animation forKey:nil];
      [self.activityView stopAnimating];
      [self.activityView removeFromSuperview];
  
      for (UIView * view in self.subviews)
      {
          if([view isKindOfClass:[UIActivityIndicatorView class]])
              [view removeFromSuperview];
      }
  }
  - (void)saveImageWithFolderName:(NSString *)folderName AndFileName:(NSString *)fileName AndImage:(NSData *) imageData
  {
      @autoreleasepool{
      NSFileManager *fileManger = [[NSFileManager defaultManager] autorelease];
      NSString *directoryPath =  [[NSString stringWithFormat:@"%@/%@",[DImageView  applicationDocumentsDirectory],folderName] autorelease];
  
      if (![fileManger fileExistsAtPath:directoryPath])
      {
          NSError *error = nil;
          [fileManger createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:&amp;error];
      }
      fileName = [DImageView fileNameValidate:fileName];
      NSString *filePath = [[NSString stringWithFormat:@"%@/%@",directoryPath,fileName] autorelease];
  
      BOOL isSaved = [imageData writeToFile:filePath atomically:YES];
      if (!isSaved)DLog(@" ** Img Not Saved");
      }
  }
  
  + (NSString *)applicationDocumentsDirectory
  {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *basePath = ([paths count] &gt; 0) ? [paths objectAtIndex:0] : nil;
      return basePath;
  }
  
  + (UIImage *)getSavedImage :(NSString *)fileName
  {
      NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
      fileName = [DImageView fileNameValidate:fileName];
  
      NSFileManager * fileManger = [[NSFileManager defaultManager] autorelease];
      NSString * directoryPath =  [[NSString stringWithFormat:@"%@/%@",[DImageView applicationDocumentsDirectory],IMAGES_FOLDER_NAME] autorelease];
      NSString * filePath = [[NSString stringWithFormat:@"%@/%@",directoryPath,fileName] autorelease];
  
      if ([fileManger fileExistsAtPath:directoryPath])
      {
          UIImage *image = [[[UIImage imageWithContentsOfFile:filePath] retain]autorelease];
          if (image)
              return image;
          else
              return nil;
      }
      [pool release];
      return nil;
  }
  
  + (NSString*) fileNameValidate : (NSString*) name
  {
      name = [name stringByReplacingOccurrencesOfString:@"://" withString:@"##"];
      name = [name stringByReplacingOccurrencesOfString:@"/" withString:@"#"];
      name = [name stringByReplacingOccurrencesOfString:@"%20" withString:@""];
      return name;
  }
  @end
  </code></pre>
  
  <p>Everything is working fine with smooth scrolling as well as asyncImage download in background. </p>
  
  <p>The issue is when i scroll <code>UITableview</code> application memory is continuously increase and after some time i got <code>Receive memory waring</code> 2/3 time then application crash.</p>
  
  <p>When i use <code>AsyncImageView</code> class that time memory not increase and its working fine. But due to app requirement i saved all images to <code>Document Directory</code> and display from it if its available.</p>
  
  <p>i have tried with <code>@autoreleasepool</code> and <code>release</code> some variable but not getting success. </p>
  
  <p>I appreciated if any one have the solution to manage memory management.</p>
  
  <pre><code> **ARC is off in my application.** 
  </code></pre>