gpt4 book ai didi

objective-c - 应用程序不释放内存,内存使用量每次都会增加一倍

转载 作者:行者123 更新时间:2023-12-03 17:10:30 24 4
gpt4 key购买 nike

我创建了一个函数,可以帮助在 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;
}

这里的问题是,由于函数的内存使用量变得很高,如下面的屏幕截图所示:

之前 enter image description here

之后 enter image description here

一段时间后 enter image description here

注意;我正在使用ARC enter image description here

我如何调用该函数?这是:

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 方法完成时,所有自动释放对象都会被释放,这对我程序结束有何帮助?
ARC 说:“又是一个好问题!Cocoa 库在整个开发过程中都使用了自动释放池,如果您没有使用过它,那么先生,这是您的问题而不是我的问题。”

About Memory Management

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com