我创建了一个函数,帮助查找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;
}
这里的问题是因为功能内存使用率变高,如下面的屏幕截图所示:
我如何称呼这个功能?这是:
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]]
}
答案 0 :(得分:1)
请在@ 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说,&#34;好的问题,它将在自动发布池结束(发布)后发布&#34;。
自动发布池何时结束?
ARC说,&#34;只要检查你在哪里创建了自动释放池,如果你还没有创建它,那么main.m
文件中有一个主要的自动释放池&#34;
这意味着当main方法完成时会释放所有自动释放对象,这对程序结束有什么帮助?
ARC说,&#34;再好的问题! Cocoa库在开发过程中使用了自动释放池,如果你还没有使用它,那么先生,你的问题不是我的。&#34;