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 8495431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:34:20+00:00 2026-06-10T23:34:20+00:00

NSFileManager* fileManager = [NSFileManager defaultManager]; NSURL* url = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject]; NSString* directory

  • 0
NSFileManager* fileManager = [NSFileManager defaultManager];
NSURL* url = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
NSString* directory = [url path];
NSString* filePath = [directory stringByAppendingPathComponent:FILE_NAME];
if ([fileManager fileExistsAtPath:filePath])
{
    [fileManager removeItemAtPath:filePath error:nil];
}   

Here’s my code. When it is executed, the file is deleted, but the space remains occupied. Here’s the code for storing something into the file.

NSFileManager* fileManager = [NSFileManager defaultManager];
NSURL* url = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
NSString* directory = [url path];
NSString* filePath = [directory stringByAppendingPathComponent:FILE_NAME];
NSArray* oldArray = nil;
if ([fileManager fileExistsAtPath:filePath])
{
    oldArray = [[NSArray alloc] initWithContentsOfFile:filePath];
    [fileManager removeItemAtPath:filePath error:nil];
}
NSMutableArray* mergeArray = [[NSMutableArray alloc] initWithArray:arrayOfPersons];
[mergeArray addObjectsFromArray:oldArray];
if ( [mergeArray writeToFile:filePath atomically:YES]) NSLog(@"Written");

By the way, it cost 1 MB to store an array with only 1 object(an NSDictionary with 2 keys). Is there a cheaper way to store it?

  • 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-06-10T23:34:22+00:00Added an answer on June 10, 2026 at 11:34 pm

    You need to be much more careful with your experiments. The unix file system does lots of stuff with files. In fact, when you “delete” a file, all you do is unlink it from the file system. If that file is open with another file descriptor, anywhere in the OS, it will remain open.

    Furthermore, there are lots of optimizations to reuse file nodes. Just because you delete a file, does not mean that space goes back automatically. It could be “reserved” in your app for several reasons, for other files to use. No sense giving it back to the file system until the file system needs it.

    settings->general->usage is a very rough measurement of file system utilization. A better measurement would be accessing the attributes of the file and file system directly.

    Using your code as a base, consider this:

    - (NSString*)workingDirectory {
        NSFileManager* fileManager = [NSFileManager defaultManager];
        NSURL* url = [[fileManager URLsForDirectory:NSCachesDirectory
                                          inDomains:NSUserDomainMask] lastObject];
        return [url path];
    }
    
    - (NSString*)filePath {
        return [[self workingDirectory] stringByAppendingPathComponent:FILE_NAME];
    }
    

    Now, you can see all the attributes of the entire file system with this:

    NSDictionary *attributes = [[NSFileManager defaultManager]
        attributesOfFileSystemForPath:[self workingDirectory] error:0];
    NSLog(@"file system attributes: %@", attributes);
    

    and those for the specific file with this:

    NSDictionary *attributes = [[NSFileManager defaultManager]
        attributesOfItemAtPath:[self filePath] error:0];
    NSLog(@"file attributes: %@", attributes);
    

    Pay attention to NSFileSystemFreeSize and NSFileSize.

    Run your app, and dump both of these values. Create your file, and dump them again. Delete the file, and dump them again.

    After all that, you may actually see the NSFileSystemFreeSize go UP, even after the delete. Remember, the system itself is creating temporary files, and is probably caching those file system nodes for future use.

    You can get more consistent results if you quit all other apps. Then, quit yours (double-click power button, X all running apps). Delete the file before doing this.

    1. Now, start your app, without the file existing.

    2. Dump file system data.

    3. Create the file.

    4. Dump file system data.

    5. Dump file data.

    You should see the file taking up about 200-250 bytes, and the file system free size should drop 8192.

    1. Delete the file.

    2. Dump file system data. Is probably at least as big as it was before deleting file.

    3. Quit app (not in XCode — double-click power, X the app).

    4. Run the app.

    5. Dump file system data. You should see the data back to about what it was when you started earlier.


    In conclusion, while it may look like the file system has not released the data, it really has, but maybe the tool you are using to query just does not know the details of the file system.

    Note, also, that when an app is running, it will use lots of file system resources for stuff that you are not explicitly doing.

    I hope that made sense…

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

Sidebar

Related Questions

NSFileManager* fileManager = [NSFileManager defaultManager]; NSURL* url = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject]; NSLog(@%@,url); NSString*
+ (void)findAndCopyOfDatabaseIfNeeded{ NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [path objectAtIndex:0]; NSFileManager
I have the following code: NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *path1 = [[NSBundle
-(IBAction)click; { NSURL *url = [NSURL URLWithString:URL_TO_DOWNLOAD]; NSString *tempDownloadPath = [[self documentsDirectory] stringByAppendingPathComponent:@test.pdf]; ASIHTTPRequest
I have implemented coredata in my app. NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentsDirectory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; NSFileManager *fileManager
Is there any way to find the parent directory of a path using NSFileManager
What's wrong with that? #define AUDIO_NOTES_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@Documents/myApp/Pictures] NSFileManager *NSFm= [NSFileManager defaultManager]; BOOL isDir=YES;
BOOL success; NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
SO i have this - (void)loadView { BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager];

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.