0

I have simple code that inserts values into my database:

-(void)inserirBd:(NSString *)valores{

    sqlite3_stmt *statement;
    // Get the documents directory
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = dirPaths[0];

    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"fazerbem.sqlite"]];
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) {

        NSString *insertSQL = valores;

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(myDatabase, insert_stmt,
                           -1, &statement, NULL);


        if (sqlite3_step(statement) == SQLITE_DONE) {
        } else {
            NSLog(@"Error -> %s", sqlite3_errmsg(myDatabase));
        }

        sqlite3_finalize(statement);
        sqlite3_close(myDatabase);
    }
}

and I am calling this function in this way:

cInsert = [NSString stringWithFormat:@"INSERT INTO fazerbem_products (value) Values ('%@')", p1_1.text,];

    InsertBD *insert = [[InsertBD alloc] init];
    [insert inserirBd:cInsert];

To perform some tests of memory, I put the above command in a loop that will insert a repeat this 85,000 times, in debug memory, before inserting the values ​​into the database the memory was 1.1 MB, after performing the loop the memory rose to around 500 MB.

But after that, why not the memory lowered to 1.1 MB again? since the loop was already processed and in this case the memory should be released ? (I'm using ARC, and my app only have a button to do this)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Perhaps there's a way to force garbage collection? – Cilan May 31 '14 at 14:37
  • I don`t know @TheWobbuffet, I`m a beginner in Objective-C, and in my logic The memory should decrease no? – user3613119 May 31 '14 at 14:39
  • Yeah... there must be something you're missing in your code... unfortunately, I know merely nothing in objective-c – Cilan May 31 '14 at 14:42
  • How are you looking at the memory? – zaph May 31 '14 at 14:46
  • I do not understand your question very well @Zaph , but believe me starting the moment it is entered in the loop I get 500MB of memory used, but just when the memory should not be released? the memory that appears in debug should not be decreased? – user3613119 May 31 '14 at 14:50
  • @Zaph, I go to run my app, and go to 'show the debug navigator', and Xcode show to me the CPU and Memory in a graph, in insert the CPU go to 0% to 98%, and when finish the CPU go to 0% again, but the memory not! – user3613119 May 31 '14 at 15:12
  • @Zaph Xcode call this tab to 'memory report' – user3613119 May 31 '14 at 15:15
  • A) Do you have an @autorelease block set up? B) Those tools are pretty flaky at best. – Hot Licks May 31 '14 at 15:54

1 Answers1

1

The Xcode Debug Memory report is not what either you want nor what you think it is.

The Debug Navigator shows the same things as the "Activity Monitor" instrument. It is not showing current live memory by your app, it's showing current memory allocated to your app by the OS. The OS will reclaim unused but allocated memory as needs arise.

The concept is to avoid allocation memory blocks from the system, deallocating only to need to re-allocate later. There is a difference between the system allocation memory (large blocks) to an app and the app allocation memory to an object (portions of the large blocks). You care about the second, the system cares about the first.

See this SO Answer Here by @Putz1103 for complete details.

Use Instruments and look at Live Bytes to see the actual memory usage.

Community
  • 1
  • 1
zaph
  • 111,848
  • 21
  • 189
  • 228