【问题标题】:Can not clean up the memory after deleting all the files from specified directory删除指定目录下的所有文件后无法清理内存
【发布时间】:2016-01-02 00:00:48
【问题描述】:

简介
在我的项目中,我必须删除文件夹及其内容,所以我尝试使用这个接受的答案ClickHere

它有效,我认为任务已经结束,但在删除整个文件夹(目录)后,我看到内存仍然分配但文件不存在。
这是我删除目录(文件夹)的代码.

-(BOOL)removeItem:(NSString*)name{
    //name: is directory(folder)'s name
    NSString*path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    path = [path stringByAppendingPathComponent:@"Normal"];
    path = [path stringByAppendingPathComponent:name];
    NSError* err;
    NSArray* list = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:&err];
    if(err){
        NSLog(@"\n##CONTENTS OF DIRECTORY ERROR:->\n%@",err.localizedDescription);
    }
    //if array's count is not zero(0) it means directory has files available.
    if(list.count >= 1){
        for(NSString* string in list){
            [[NSFileManager defaultManager]removeItemAtPath:[path stringByAppendingPathComponent:string] error:&err];
            if(err){
                NSLog(@"\n##FOLDER IN FOLDER ERROR:->\n%@",err.localizedDescription);
            }
        }
        [[NSFileManager defaultManager]removeItemAtPath:path error:&err];
        return YES;
    }else{
        [[NSFileManager defaultManager]removeItemAtPath:path error:&err];
        if (err) {
            return NO;
        }
        return YES;
    }
}


提前致谢。

【问题讨论】:

  • 请解释您所说的“内存仍在分配”是什么意思。
  • @A-Live,当我在真实设备上安装我的应用程序时。全新安装时,设置->常规->使用->管理存储中显示的应用程序大小约为6mb。比我添加一些文件(视频文件)和大小增加。但是当我删除一些文件(视频文件)(是的,文件被删除)时,我的应用程序的大小似乎没有减少。
  • 有可能空间确实被释放了,但统计信息并没有立即更新,自动刷新可能需要一些时间。也可能是统计数据被缓存并且需要以某种方式重置,如果它们没有到期日期重新启动设置或设备可能会有所帮助。如果您有证据表明文件系统由于空间不足而不允许您创建新文件,并且您认为发生这种情况是因为空间实际上并未释放,请添加有关您如何得出此结论的更多详细信息。

标签: ios objective-c xcode nsfilemanager nshomedirectory


【解决方案1】:

好的,我遇到了问题。在我的情况下,问题是一个临时文件夹(目录)。当我将视频添加到我的应用程序时,iOS 在应用程序的临时文件夹(目录)中创建了一些临时文件。因此,从我的应用程序中删除文件(视频)后,我在设置->常规->使用->管理存储中看到的应用程序大小是临时文件夹的大小。
因此,当您从图库中导入照片或视频时,您必须在获取后手动清除临时文件夹。我正在从 UIImagePickerController 的委托中清除临时文件夹,如下所示。

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
[self dismissViewControllerAnimated:YES completion:^{
BOOL success = [videoData writeToFile:path atomically:YES];
            NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
            [RWTAppDelegate clearTmpDirectory];
}];
}


+ (void)clearTmpDirectory
{
    NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
    for (NSString *file in tmpDirectory) {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
    }
}


swift 3+ 更新

class func clearTmpDirectory(){
        let path = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
        let manager = FileManager.default
        let files = try? manager.contentsOfDirectory(atPath: path.path)
        files?.forEach { (file) in
            let temp = path.appendingPathComponent(file)
            try? manager.removeItem(at: temp)
            // --- you can use do{} catch{} for error handling ---//
        }
}


对不起我的英语不好。

【讨论】:

  • 1 起来!如果把它留在这里会更好: - (void)applicationWillTerminate:(UIApplication *)application {}
  • @JiulongZhao,是的,你是对的,但在我的情况下,这会在一段时间后产生存储空间问题,因为我必须经常添加视频,而且我等不及“-(void)applicationWillTerminate: (UIApplication*)application{}"
  • 非常感谢。真的很烦人的 UIImagePickerController 在它自己之后并没有清理,但它没有清理是有道理的,因为返回的文件存储在 NSTemporaryDirectory 中。我建议在 applicationWillTerminate 之前不要删除 NSTemporaryDirectory 文件,因为我发现了困难的方法,尽早删除这些文件,图像选择器仍然返回删除文件路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
  • 1970-01-01
相关资源
最近更新 更多