memory leaks - In what android activity state should one be opening and closing database connections? -
i have commercial application finished. has 8 list activities , 4 detail activities , bunch of other stuff going on.
when started out (and new android development), in each method needed database connection, created new sqliteopenhelper subclass instance , database connection , closed connection @ end of method. seemed bad things.
complicating need install data upon first launch. have rest app access , have cached data it. seem run out of memory when try load 1 of tables when use jsonreader class in gson , not regular inputstreamreader functionality. so, supposed read in objects 1 @ time. still runs out of memory on emulators <= 1 gb memory.
so, started using singleton, synchronized getter, sqliteopenhelper subclass instance. doing this, should still able db connection objects , close them within methods? did not seem work.
now using simplecursoradapter subclass list activities. seems move problem bit not fix or make worse.
see diagram of activities , when run:
http://developer.android.com/images/activity_lifecycle.png
seeing this, think should able create database connection in onstart call , close , null db connection object in onstop call. no? when using simplecursoradapter subclass, loading adapter in onstart call , changing cursor null in onstop call. wrong? apparently is.
i have detail activity db connection ivar in class scope. open db connection in onstart , close in onstop. so, why getting "java.lang.illegalstateexception: attempt re-open already-closed object: sqlitedatabase" error in onclick method? onclick gets called between onstart , onstop calls, yes? heck gives?
right have helper method takes db connection instance parameter , checks , either returns it, if open, or sends new, open instance if not.
so, should need call method every 5 or 10 lines in code? not think so, how feels. only closing db connection objects in onstop methods. why should problem? should not close anything? leak, may not crash. seems wrong. hmph.
how many db connection objects have? 1 in entire app? 1 per thread? other number? how close them not leaked without causing crashes?
any other suggestions?
database class extends sqliteopenhelper.
in case singleton work hole application.
each activity needs access database create instace of appropiated database class ie. categories.
in constructor of categories class have:
public databasecategory(){ db = databasestorage.get(app.getcontext()).getsqlitedatabase(); }
so first activity needs access db create/open o open database , remain opend until aplication finishes.
singleton:
public class databasestorage { private static final string tag = databasestorage.class.getsimplename(); private static database database = null; private databasestorage(){ } public static database get(context context){ if (database == null){ database = new database(context); try { log.d(tag, "calling createdatabase method"); database.createdatabase(); } catch (ioexception ioe) { throw new error("unable create database"); } try { log.d(tag, "calling opendatabase method"); database.opendatabase(false); }catch(sqlexception sqle){ throw sqle; } } if (database.isopen() == false){ try { log.d(tag, "calling opendatabase method"); database.opendatabase(false); }catch(sqlexception sqle){ throw sqle; } }else{ log.d(tag, "returning previous database"); } return database; } }
Comments
Post a Comment