title: iOS check MD5 lead to memory warning
body: <p>I have used some ways to check MD5 after downloading files. But, it seems that checking too much files md5 always lead to memory warning or crash for low memory. Does someone have a good solution for this?</p>
  
  <p>I have use method to check MD5 from <a href="http://www.joel.lopes-da-silva.com/2010/09/07/compute-md5-or-sha-hash-of-large-file-efficiently-on-ios-and-mac-os-x/" rel="nofollow">Joel's Writings</a>, and other methods from other posts, but all could not solve my problem.</p>
  
  <pre><code>NSInteger index = 0;
  
      while (downloadQueue.count &gt; 0) {
  
          NSLog(@"%i files need to download.", downloadQueue.count);
          NSLog(@"00: %u", [MenChecker get_free_memory]);
  
          @autoreleasepool {
  
              NSString *src = [downloadQueue objectAtIndex:index];
              NSString *path = [self.storageRoot stringByAppendingPathComponent:[storageList objectAtIndex:index]];
              NSString *md5 = [md5CheckList objectAtIndex:index];
  
              BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:path];
              if (isExist) {
  
                  NSString *md5OfData = nil;
  
                  CFStringRef fileMD5Hash = FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
                  if (fileMD5Hash) {
  
                      md5OfData = (__bridge NSString *)fileMD5Hash;
                      CFRelease(fileMD5Hash);
                  }
  
                  if ([md5OfData isEqualToString:md5]) {
  
                      [downloadQueue removeObjectAtIndex:index];
                      [storageList removeObjectAtIndex:index];
                      [md5CheckList removeObjectAtIndex:index];
                  }
                  else {
  
                      [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
  
                      ASIHTTPRequest *request_cover = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:src]];
                      [request_cover setDownloadDestinationPath:path];
                      [request_cover startSynchronous];
  
                      NSString *md5OfDataFile = nil;
                      CFStringRef dataFileMD5Hash = FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
                      if (dataFileMD5Hash) {
  
                          md5OfDataFile = (__bridge NSString *)dataFileMD5Hash;
                          CFRelease(dataFileMD5Hash);
                      }
  
                      if ([[NSFileManager defaultManager] fileExistsAtPath:path] &amp;&amp; [md5OfDataFile isEqualToString:md5]) {
  
                          [downloadQueue removeObjectAtIndex:index];
                          [storageList removeObjectAtIndex:index];
                          [md5CheckList removeObjectAtIndex:index];
                      }
                      else {
  
                          index ++;
                      }
                      md5OfDataFile = nil;
                  }
                  md5OfData = nil;
              }
              else {
  
                  NSString *directory = [path stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"/%@", [path lastPathComponent]] withString:@""];
                  [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
  
                  ASIHTTPRequest *request_cover = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:src]];
                  [request_cover setDownloadDestinationPath:path];
                  [request_cover startSynchronous];
                  request_cover = nil;
  
                  NSString *md5OfData = nil;
                  CFStringRef fileMD5Hash = FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
                  if (fileMD5Hash) {
  
                      md5OfData = (__bridge NSString *)fileMD5Hash;
                      CFRelease(fileMD5Hash);
                  }
  
                  if ([[NSFileManager defaultManager] fileExistsAtPath:path] &amp;&amp; [md5OfData isEqualToString:md5]) {
  
                      [downloadQueue removeObjectAtIndex:index];
                      [storageList removeObjectAtIndex:index];
                      [md5CheckList removeObjectAtIndex:index];
                  }
                  else {
  
                      index ++;
                  }
                  md5OfData = nil;
              }
  
              if (index &gt;= downloadQueue.count) {
  
                  index = 0;
              }
          }
  </code></pre>