应用程序不释放内存,内存使用每次都增加一倍
App not releasing the memory, Memory use is getting doubled every time
我创建了一个函数来帮助查找 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;
}
这里的问题是由于函数内存使用越来越高,如下面的屏幕截图所示:
之前
之后
一段时间后
注意;我正在使用 ARC
如何调用函数?这里是:
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 一个对象,如果对象的保留计数变为立即释放,但是如果你自动释放一个对象,一旦自动释放池是 released/drained 释放消息就会被发送,即释放被推迟。举个例子。
- (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
我创建了一个函数来帮助查找 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;
}
这里的问题是由于函数内存使用越来越高,如下面的屏幕截图所示:
之前
之后
一段时间后
注意;我正在使用 ARC
如何调用函数?这里是:
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 一个对象,如果对象的保留计数变为立即释放,但是如果你自动释放一个对象,一旦自动释放池是 released/drained 释放消息就会被发送,即释放被推迟。举个例子。
- (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 图书馆在整个开发过程中都使用了自动释放池,如果您还没有使用它,那么先生,这是您的问题,而不是我的问题。”