memory free question

I created a delete file method, calls it through NSThread. Through Activity Monitor examined that the actual memory takes 100M,but why this thread after completion has not released any memory to take the memory not to reduce actually?

code:



-(void)removeFilesThread:(NSDictionary *)parameter

{

@autoreleasepool

{

NSString *filePath=[parameter objectForKey:@"FilePath"];

NSString *fileType=[parameter objectForKey:@"FileType"];

NSUInteger fileSaveDays=[[parameter objectForKey:@"FileSaveDays"] unsignedIntegerValue];

NSUInteger fileSaveCount=[[parameter objectForKey:@"FileSaveCount"] unsignedIntegerValue];

NSFileManager *fileManage=[NSFileManager defaultManager];

NSArray *filesName=[fileManage contentsOfDirectoryAtPath:filePath error:nil];

NSMutableDictionary *sortFileTypeDic=[[NSMutableDictionary alloc] init];

for (NSString *fileName in filesName)

{

NSString *pathExtension=[fileName pathExtension];

NSArray*fileTypes=[fileType componentsSeparatedByString:@"|"];

if ([fileTypes containsObject:@"all"]||[fileTypes containsObject:pathExtension])

{

NSString *fileFullPath=[filePath stringByAppendingPathComponent:fileName];

NSDictionary *fileAttributes=[fileManage attributesOfItemAtPath:fileFullPath error:nil];

NSDate *fileModifDate=[fileAttributes objectForKey:NSFileModificationDate];

if (fileModifDate)

{

NSMutableDictionary *filesModifDateDic=([LLKStatic estimateIdClass:[NSDictionary class] idValue:[sortFileTypeDic objectForKey:pathExtension]])?[[NSMutableDictionary alloc]initWithDictionary:[sortFileTypeDic objectForKey:pathExtension]]:[[NSMutableDictionary alloc] init];

[filesModifDateDic setObject:fileModifDate forKey:fileFullPath];

[sortFileTypeDic setObject:filesModifDateDic forKey:pathExtension];

}

}

}

for (NSString *fileType in sortFileTypeDic)

{

NSMutableDictionary *filesModifDateDic=[LLKStatic estimateIdClass:[NSDictionary class] idValue:[sortFileTypeDic objectForKey:fileType]];

NSArray *filesBySortValue=[filesModifDateDic keysSortedByValueUsingSelector:@selector(compare:)];

if (filesBySortValue.count>fileSaveCount)

{

NSArray *removeSaveFiles=[filesBySortValue subarrayWithRange:NSMakeRange(0, filesBySortValue.count-fileSaveCount)];

for (NSString *fileFullPath in removeSaveFiles)

{

NSDate *modifiDate=[LLKStatic estimateIdClass:[NSDate class] idValue:[filesModifDateDic objectForKey:fileFullPath]];

if (modifiDate)

{

NSDate *nowDate=[NSDate date];

NSInteger distanceDays=(NSInteger)[nowDate timeIntervalSinceDate:modifiDate]/86400;

if (distanceDays>=fileSaveDays)

{

[fileManage removeItemAtPath:fileFullPath error:nil];

}

}

}

}

}


}

}

Replies

You need to differentiate between allocated memory and memory as tracked by the VM subsystem. Memory that was allocated within

-removeFilesThread:
should be freed when the thread terminates. If not, that’s a specific issue that you need to investigate.

The tool of choice for looking at problems with allocated memory is the Allocations instrument.

Activity Monitor shows memory from the VM subsystem’s perspective. It can be tricky to hard to map operations within your app to changes in VM subsystem. For example, calling

malloc
to allocate a very large memory block will consume virtual memory but no physical memory (until you actually touch the pages). Similarly, freeing a bunch of small allocations may not return either virtual memory or physical memory to the VM subsystem.

I recommend you start your memory investigation by looking at allocations, and only move on to looking at the VM subsystem when you’ve got the allocations under control.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks your recommendation and the answer.It has the help very much to me.