我创建了一个有助于在NSString数组中查找重复文件的函数(数组包含文件路径)

这是我的功能:

-(NSMutableArray *)compareWithList:(NSMutableArray*)fileCompareList // list of file from which we need to find the duplicates of the target
                          fileData:(NSData*)_fileData // contains target file data bytes
                        fileLength:(UInt32) _fileLenght // contains size of target file
{
    // result list of the (duplcate)files
    NSMutableArray* fileList = [[NSMutableArray alloc]init];

    // flag to check if the path is a folder
    BOOL isDir;

    //stores the size of the file that is being itrated
    UInt32 size = 0;
    //stores the byte data of the file that is being itrated
    NSData *bytes = nil;

    //itrating the files in the list one by one in order find the duplicate
    for (NSString* sPath in fileCompareList) {
        //checking if the file already exists in the result list
        if ([fileList indexOfObject:sPath] == NSNotFound)
        {
            //getting the size of the file
            UInt32 size = [sysHelp getSizeOfFile:sPath];
            //if the size matches of the target file and the itrated file then go inside
            if(size == _fileLenght)
            {
                //get the bytes of the file being itrated
                bytes = [[NSData alloc] initWithContentsOfFile:sPath];

                //if the bytes matches then add the itrated file into the result list
                if([bytes isEqualToData:_fileData])
                {
                    [fileList addObject:sPath];
                }
                //remove the itrated file data from the array
                bytes = nil;
            }
        }

    }

    return fileList;
}


这里的问题是,由于该功能,内存使用率越来越高,如下图所示:

之前
objective-c - 应用程序未释放内存,每次使用的内存都增加了一倍-LMLPHP


objective-c - 应用程序未释放内存,每次使用的内存都增加了一倍-LMLPHP

一段时间后
objective-c - 应用程序未释放内存,每次使用的内存都增加了一倍-LMLPHP

注意;我正在使用ARC
objective-c - 应用程序未释放内存,每次使用的内存都增加了一倍-LMLPHP

我怎么称呼功能?这里是:

NSMutableArray* allFilesOfSystem =[[NSMutableArray alloc] init];
allFilesOfSystem = self AllFilesOfDesktopAndSubDirectores];

NSMutableArray* FinalResultArray = [[NSMutableArray alloc] init];

for (int i = 0; i < [allFilesOfSystem count]; i++) {

        NSString* filePath = [allFilesOfSystem objectAtIndex:i];

        //file to byte array
        NSData *bytes = [[NSData alloc] initWithContentsOfFile:filePath];

        //file size
        UInt32 size = [sysHelp getSizeOfFile:filePath];

        [FinalResultArray addObjectsFromArray:[self compareWithList:allFilesOfSystem fileData:bytes fileLength:size]]

}

最佳答案

请在@ autoreleasepool中移动所有语句,如果有帮助请告诉我。下面是代码

-(NSMutableArray *)compareWithList:(NSMutableArray*)fileCompareList // list of file from which we need to find the duplicates of the target
                          fileData:(NSData*)_fileData // contains target file data bytes
                        fileLength:(UInt32) _fileLenght // contains size of target file
{
  // result list of the (duplcate)files
  NSMutableArray* fileList = [[NSMutableArray alloc] init];

  @autoreleasepool {
    // flag to check if the path is a folder
    BOOL isDir;

    //stores the size of the file that is being itrated
    UInt32 size = 0;
    //stores the byte data of the file that is being itrated
    NSData *bytes = nil;

    //itrating the files in the list one by one in order find the duplicate
    for (NSString* sPath in fileCompareList) {
      //checking if the file already exists in the result list
      if ([fileList indexOfObject:sPath] == NSNotFound)
      {
        //getting the size of the file
        UInt32 size = [sysHelp getSizeOfFile:sPath];
        //if the size matches of the target file and the itrated file then go inside
        if(size == _fileLenght)
        {
          //get the bytes of the file being itrated
          bytes = [[NSData alloc] initWithContentsOfFile:sPath];

          //if the bytes matches then add the itrated file into the result list
          if([bytes isEqualToData:_fileData])
          {
            [fileList addObject:sPath];
          }
          //remove the itrated file data from the array
          bytes = nil;
        }
      }

    }
  }
  return fileList;
}




编辑:说明

放弃对象的所有权有两种方法。一个是发布,另一个是自动发布。如果您对某个对象调用release,并且该对象的保留计数变为立即释放,但是如果您自动释放对象,则一旦释放/耗尽了自动释放池即延迟释放,就会发送释放消息。让我们举个例子。

- (void)testMemoryInARC1 {
  NSData *data = [[NSData alloc] initWithContentsOfFile:@"myfile.mp3"]; // reatin count is 1
  //used this data an after few statements
  //statement 1
  // ..


  //at the end of data varibale scope in this case it's the end of method
  //ARC will insert the release call
  // [data rlease]; //wchich releases the memory
}


- (void)testMemoryInARC2 {
  NSData *data = [NSData dataWithContentsOfFile:@"myfile.mp3"]; // this method returns the autorelase object
  //used this data an after few statements
  //statement 1
  // ..


  //at the end of data varibale scope in this case it's the end of method
  //ARC will NOT insert the release call since that was autoreleased object
  //hence no release call
}


那么问题来了,它将何时发布?
ARS说:“很好的问题,当自动发布池结束(发布)时,它将发布”。

自动发布池何时会结束?
ARC说:“只要检查您在哪里创建了自动释放池,如果尚未创建,那么main.m文件中会有一个主要的自动释放池”

这意味着当main方法完成时,所有的autorelease对象都会被释放,这对程序的结尾有什么帮助?
ARC说:“再好的一个问题!可可库在整个开发过程中都使用了自动释放池,如果您还没有使用过它,那是您的问题,而不是我的。”

About Memory Management

关于objective-c - 应用程序未释放内存,每次使用的内存都增加了一倍,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32818809/

10-15 11:17