Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory issue in ARC

I am using UINavigationcontroller as root view controller. I am new to iphone ARC and sqlite operations under ARC.

  1. My rootviewcontroller is loginviewcontroller next is homeview which contains scrollView with 4 UIviewcontroller in it.
  2. I am using Instruments to test as I am getting a memory warning. After using my app for about 5-10 minutes, when I logout (for this I am using popToRootViewController) and login again Live Bytes goes on increasing and when I check Call Trees -

     [NSObject(NSTreadPerformAdditions) performselectorOnMainThread:withObject:waituntilDone:] 
    

shows 15.91MB Bytes used and following it shows

I am also doing SQLite operations and upon performing those operations, it adds around

if(sqlite3_open([[self filePath]UTF8String],&dBObject)!= SQLITE_OK)

shows 10MB Live Bytes for opendatabase, selectquery and update

For the first time I tested my app using Instruments, it showed the following: enter image description here

How can i release this memory now?

EDIT:

i am using this code for retriving data

-(int)CheckCompanyIsAvailableInTradeList:(NSString *)CompanyId UserId:(NSString *)userId
{
int check=0;
if(sqlite3_open([[self filePath] UTF8String], &dBObject) == SQLITE_OK) {
    NSString *sqlStatement =NULL;
    sqlStatement = [ NSString stringWithFormat:@"SELECT COUNT(*) FROM tradelist WHERE compnyID = '%@' and UserMasterID ='%@' ;",CompanyId,userId];

    if(sqlite3_prepare_v2(dBObject, [sqlStatement UTF8String], -1, &statement, NULL) == SQLITE_OK) {
        @autoreleasepool {
            while(sqlite3_step(statement) == SQLITE_ROW) {
                check = sqlite3_column_int(statement, 0);

            }
        }



    }
    sqlite3_finalize(statement);

}
sqlite3_close(dBObject);
return check;

}

whenever this method is called live bytes goes on increasing continously

like image 732
the1pawan Avatar asked Nov 02 '22 21:11

the1pawan


1 Answers

Try this,

Before you start your loop you have to open database...

and after completing you have to close your database..

e.g.

 -(void)downloadData
 {
 [self  openDB];// database is open..

 // start your loop 
for (int i=0; i<[array count]; i++)
{
   [self executeInsertQuery];

}

[self  closeDb]; 
 }


-(void)openDB
{
 if(sqlite3_open([[self filePath]UTF8String],&dBObject)!= SQLITE_OK)
{
    sqlite3_close(dBObject);
}

} 


-(void)closeDb
{
if (dBObject)
{

    int rc = sqlite3_close(dBObject);
    NSLog(@"close rc=%d", rc);

    if (rc == SQLITE_BUSY)
    {

        sqlite3_stmt *stmt;
        while ((stmt = sqlite3_next_stmt(dBObject, 0x00)) != 0)
        {
            NSLog(@"finalizing stmt");
            sqlite3_finalize(stmt);
        }

        rc = sqlite3_close(dBObject);
    }

    if (rc != SQLITE_OK)
    {
        NSLog(@"close not OK.  rc=%d", rc);
    }

    dBObject = NULL;
}

}

  -(Bool)executeInsertQuery
{


  NSString* strQuery=[NSString stringWithFormat: @"Insert into   list(compnyID,UserMasterID) values('%@','%@');",companyId ,userId ];

 char *err;
if (sqlite3_exec(dBObject, [queryString UTF8String], NULL,NULL, &err)!= SQLITE_OK)
{
    sqlite3_close(dBObject);
    return NO;
}
else
{
    return YES;
}


}
like image 182
Kalpesh Avatar answered Nov 15 '22 06:11

Kalpesh