Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6223351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:28:28+00:00 2026-05-24T08:28:28+00:00

I wrote a method(getDirTree1) that makes listing all directories from the root, using the

  • 0

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 ); 
}

}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:28:30+00:00Added an answer on May 24, 2026 at 8:28 am

    [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.

    (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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a method that extracts fields from an object like this: private static
Using an idea from Bob King idea I wrote the following method. It works
I wrote a method that deletes rows from my an asp.net Gridview when the
I wrote a method that gathers data from an Oracle server, formats and encrypts
I wrote a method (that works fine) for a() in a class. I want
I have and old(ish) C# method I wrote that takes a number and converts
Hello StackOverflow community, Using Google App Engine, I wrote a keyToSha256() method within a
I wrote a windows service in C# that does some functionality (a public method)
I wrote a method which exports values to excel file from an IEnumerable parameter.
i have wrote a method that filters output with provided query and return it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.