【问题标题】:Cleared the files but memory is not reduced清除了文件但内存没有减少
【发布时间】:2018-07-16 07:12:14
【问题描述】:

我正在创建一个应用程序。我将文件存储在 Document 目录中。在我的工作完成后,从文档目录中删除文件,如下所示:

    NSMutableDictionary * Dictionary = [NSMutableDictionary alloc]init]; 
    // Next every file storing into this dictionary like below 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *baseDir = [paths objectAtIndex:0]; 
    NSString *pathComp = [baseDir stringByAppendingPathComponent:[NSString stringWithFormat:@"IMG%d.PNG",presentCount]; 
    fileURL = [NSURL fileURLWithPath:pathComp]; 
    [Dictionary setObject:fileURL forKey:fileURL]; 

    while ([[Dictionary allKeys]count]!=0) {
        NSURL *deleteFileURL = [[Dictionary allKeys] lastObject];
        NSLog(@"Path %@",deleteFileURL.path);
        [[NSFileManager defaultManager] removeItemAtPath:deleteFileURL.path error:nil];
        [Dictionary removeObjectForKey:deleteFileURL];
    }

我的问题是,从文档目录中删除文件后,内存并没有减少,仍然像文件一样占用。由于这个问题,我的崩溃了。所以请帮助我如何清除内存。

实际上,我从服务器获取文件(照片)并首先放置在文档目录中,并尝试使用照片库保存。一旦我将字典中的输入提供给照片库,在完成处理程序之后,我试图删除文件。已删除并保存照片,但内存并未减少。

【问题讨论】:

  • 请提供声明和初始化字典变量的代码行
  • 字典 = [NSMutableDictionary alloc]init];接下来每个文件都存储到这个字典中,如下所示 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *baseDir = [paths objectAtIndex:0]; NSString *pathComp = [baseDir stringByAppendingPathComponent:[NSString stringWithFormat:@"IMG%d.PNG",presentCount]; fileURL = [NSURL fileURLWithPath:pathComp]; [字典 setObject:fileURL forKey:fileURL];
  • 我注意到您在文件名中使用了大写字母。您应该考虑到,iOS 文件系统区分大小写,这可能会导致问题。
  • 这有什么问题吗?
  • 请检查removeItemAtPath的返回值。还要为要返回的错误添加一个参数。

标签: ios objective-c memory memory-management


【解决方案1】:

1、检查路径上是否存在文件后删除文件,并检查removeItemAtPath:的返回值判断是否删除成功。

NSString *path = @"a/b";
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
       BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
       // Check the success's value
    }

【讨论】:

  • 这个也试过了,总是返回1,但是内存没有被清除。
猜你喜欢
  • 1970-01-01
  • 2010-11-22
  • 2012-12-27
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 2018-01-28
  • 2015-11-30
相关资源
最近更新 更多