简体   繁体   中英

sqlite orientation change memory leak

I have a sqlite method that runs once, on oncreate and it's the following one:

List<Map<String, String>> resultsMap = new ArrayList<>();

        String sqlQuery = " SELECT city_id,city_name,population FROM citylist ";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c          = db.rawQuery(sqlQuery, null);

        int i = 0;
        //Loop through the results and add it to the temp_array
        if (c.moveToFirst()){

            do{
                //Answer Map
                Map<String, String> map = new HashMap<String, String>();
                map.put("cid",       c.getString(c.getColumnIndex("city_id"))  );
                map.put("cname",     c.getString(c.getColumnIndex("city_name"))  );
                map.put("population",    c.getString(c.getColumnIndex("population"))  );
                //Add Answer to Answers arraylistmap
                resultsMap.add(i, map);
                i++;

            } while(c.moveToNext());

        }

        //Close the cursor
        c.close();
        db.close();

        //Return the string array
        return theresults;

All runs well, but the problem is that when I rotate the emulator a few times on the screen, it runs out of memory.

I call it on onCreate like this

citiesList = myDbHelper.citiesListDB();
autoCompleteSearch.setThreshold(1);
searchAdapter = new SearchableAdapter(this, citiesList);
autoCompleteSearch.setAdapter(searchAdapter);

autoCompleteSearch is an AutoCompleteTextView view, that has a dropdown that is populated from the ArrayList that is returned from the db method.


I did my best to close the db connection, but the app crashes nonetheless with this error

E/dalvikvm-heap: Out of memory on a 28-byte allocation.
I/dalvikvm: "main" prio=5 tid=1 RUNNABLE
I/dalvikvm:   | group="main" sCount=0 dsCount=0 obj=0xb5e5a4b0 self=0xb8b1a4e0
I/dalvikvm:   | sysTid=11362 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1208536000
I/dalvikvm:   | schedstat=( 0 0 0 ) utm=40 stm=45 core=0
I/dalvikvm:     at java.util.HashMap.makeTable(HashMap.java:~555)
I/dalvikvm:     at java.util.HashMap.doubleCapacity(HashMap.java:575)
I/dalvikvm:     at java.util.HashMap.put(HashMap.java:405)
I/dalvikvm:     at DatabaseHelper.citiesListDB(DatabaseHelper.java:262)

and a this:

FATAL EXCEPTION: main
java.lang.OutOfMemoryError: [memory exhausted]
at dalvik.system.NativeStart.main(Native Method)

I think it might be something related to memory garbage or something


Update: DatabaseHelper.java:262
is

map.put("cid",       c.getString(c.getColumnIndex("city_id"))  );

You are closing the database correctly, so this most likely has nothing to do with the error. I think that you are loading too much data into memory. You should use a CursorAdapter instead of an ArrayAdapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM