如何优化目录列表? (enumeratorAtPath和递归调用contentsOfDirectoryAtPath) [英] How to optimize directory listing? (enumeratorAtPath and recursive call contentsOfDirectoryAtPath)

查看:539
本文介绍了如何优化目录列表? (enumeratorAtPath和递归调用contentsOfDirectoryAtPath)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个方法(getDirTree1),该方法使用推荐的类NSDirectoryEnumerator和方法nextObject从根目录列出所有目录.但是,当它运行时,却占用了大量内存(主要是私有类NSPathStore2):

I wrote a method(getDirTree1) that makes listing all directories from the root, using the recommended class NSDirectoryEnumerator and method nextObject. But while it was running unacceptably used a lot of memory (mainly private class NSPathStore2):

-(void) getDirTree1:(NSString*)directoryPath {
    NSDirectoryEnumerator *dirEnum = [self->fileManager enumeratorAtPath:derectoryPath];
    NSString *filePath;
    NSString *fullFilePath;

    while ( ( filePath = [ dirEnum nextObject ] ) != nil ) {
        fullFilePath = [ directoryPath stringByAppendingPathComponent:filePath ];
        NSLog( @"%@ \n", fullPath ); 
}

}

假设这是因为对象NSDirectoryEnumerator,我重写了方法(getDirTree2).现在使用递归,NSArray类和objectEnumerator方法. 但是再次占用了很多内存.

Assuming that this is because the object NSDirectoryEnumerator, I rewrote the method(getDirTree2). Now using the recursion, NSArray class and objectEnumerator method. But once again used a lot of memory.

-(void) getDirTree2:(NSString*)directoryPath {
   NSArray *contents = [ self->fileManager contentsOfDirectoryAtPath:directoryPath error:NULL ];
   NSEnumerator *enumeratorContent [ contents objectEnumerator ];
   NSString *file;
   BOOL fileIsDirectory = FALSE;

   while ( ( file = [ enumeratorContent nextObject ] ) ) {
       NSLog( @"%@ \n", [ directoryPath stringByAppendingPathComponent: file ] );
       if ( [ self->fileManager fileExistAtPath:[ directoryPath stringByAppendingPathComponent:file ] isDirectory:&fileIsDirectory ] && fileIsDirectory )
           [ self getDirTree2:[ directoryPath stringByAppendingPathComponent: file ] ];
   }
}

我错过了什么(也许我必须取消分配/保留一些对象)以及如何做得更好. 谢谢.

What I missed(maybe I must dealloc/retain some objects) and how to do better. Thanks.

推荐答案

[directoryPath stringByAppendingPathComponent:filePath];返回自动释放的对象.由于它发生在如此紧密的循环中,因此所有这些对象加起来并导致了所涉及的大内存占用.解决该问题所要做的就是更频繁地摆脱它们.您可以将方法更改为不使用自动释放的方法,也可以仅创建自己的紧密自动释放池,如下所示:

[directoryPath stringByAppendingPathComponent:filePath]; returns an autoreleased object. Since it's happening inside such a tight loop, all these objects are adding up and causing the large memory footprint in question. All you have to do to fix it is get rid of them more often. You can change your method to something that doesn't use autorelease, or you could just create your own, tight autorelease pool, like this:

while ( ( filePath = [ dirEnum nextObject ] ) != nil ) {
    NSAutoreleasePool* pool = [NSAutoreleasePool new];
    fullFilePath = [ directoryPath stringByAppendingPathComponent:filePath ];
    NSLog( @"%@ \n", fullPath );
    [pool drain];
}

这将确保您不再需要所有内容 都已发布,从而避免了循环期间对象的堆积.

This will ensure that everything's released as soon as you no longer need it, avoiding the buildup of objects during the loop.

(有趣的旁注:NSPathStore2是与NSString(这是一个类簇)相关的私有类,用于存储路径类型的字符串.这就是我知道哪种方法有问题的方式.)

(Interesting sidenote: NSPathStore2 is a private class related to NSString (which is a class cluster) used to store path-type strings. That's how I knew which method was at fault.)

这篇关于如何优化目录列表? (enumeratorAtPath和递归调用contentsOfDirectoryAtPath)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆