【发布时间】:2015-12-25 10:45:34
【问题描述】:
我创建了一个函数来帮助在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]]
}
【问题讨论】:
-
@ElTomato 你听说过自动引用计数吗?
-
天哪。我也没有问你。我正在回复 ElTomato。
-
是什么让你觉得内存被浪费了?内存使用条表示从系统获取的 RAM 量。并不是说被App占用了。如果内存使用量达到“危险”值,会发生什么?数百 MB 并不危险。
-
简单地做,@Inder 从我的 cmets 偷了什么。
-
@VikasBansal 如果 autoreleasepool 对你有用,那么用 autoreleasepool 包装调用函数代码(你最后添加到问题中)因为我看到你也在那里分配 NSData 实例。
标签: objective-c cocoa automatic-ref-counting