Android 查询数据库时 项目出现 OOM (不断引发GC)

这两天项目遇到一个棘手的问题:在查询数据库数据记录(全部显示在屏幕上)时,不断引发gc 操作,最后 oom

logcat日志如下:

02-15 10:53:25.853: I/System.out(2223): DDP: come here add one recode
02-15 10:53:30.843: I/System.out(2223): DDP: cursor.count = 3
02-15 10:53:31.273: D/dalvikvm(2223): GC freed 934 objects / 77736 bytes in 83ms
02-15 10:53:31.873: D/dalvikvm(2223): GC freed 107 objects / 29960 bytes in 88ms
02-15 10:53:32.593: D/dalvikvm(2223): GC freed 123 objects / 24176 bytes in 102ms
02-15 10:53:33.553: D/dalvikvm(2223): GC freed 44 objects / 29368 bytes in 115ms
02-15 10:53:34.813: D/dalvikvm(2223): GC freed 3 objects / 41632 bytes in 153ms
02-15 10:53:36.563: D/dalvikvm(2223): GC freed 3 objects / 62408 bytes in 218ms
02-15 10:53:38.283: D/dalvikvm(2223): GC freed 3 objects / 93576 bytes in 265ms
02-15 10:53:40.103: D/dalvikvm(2223): GC freed 2 objects / 48 bytes in 322ms
02-15 10:53:41.853: D/dalvikvm(2223): GC freed 3 objects / 140328 bytes in 325ms
02-15 10:53:43.804: D/dalvikvm(2223): GC freed 2 objects / 48 bytes in 387ms
02-15 10:53:45.932: D/dalvikvm(2223): GC freed 2 objects / 48 bytes in 556ms
02-15 10:53:47.762: I/dalvikvm-heap(2223): Clamp target GC heap from 24.386MB to 24.000MB
02-15 10:53:47.762: D/dalvikvm(2223): GC freed 3 objects / 210456 bytes in 528ms
02-15 10:53:49.462: I/dalvikvm-heap(2223): Clamp target GC heap from 26.025MB to 24.000MB
02-15 10:53:49.462: D/dalvikvm(2223): GC freed 2 objects / 48 bytes in 475ms
02-15 10:53:49.462: I/dalvikvm-heap(2223): Clamp target GC heap from 24.025MB to 24.000MB
02-15 10:53:49.472: I/dalvikvm-heap(2223): Grow heap (frag case) to 24.000MB for 26-byte allocation
02-15 10:53:50.013: I/dalvikvm-heap(2223): Clamp target GC heap from 26.025MB to 24.000MB
02-15 10:53:50.023: D/dalvikvm(2223): GC freed 0 objects / 0 bytes in 550ms
02-15 10:53:50.023: I/dalvikvm-heap(2223): Forcing collection of SoftReferences for 24-byte allocation
02-15 10:53:50.522: I/dalvikvm-heap(2223): Clamp target GC heap from 26.025MB to 24.000MB
02-15 10:53:50.522: D/dalvikvm(2223): GC freed 0 objects / 0 bytes in 500ms
02-15 10:53:50.522: E/dalvikvm-heap(2223): Out of memory on a 24-byte allocation.




找了半天,原来以为是项目中的一些大图片导致的内存吃紧。一直没有找到问题的症结所在。

今天自己写了一个操作数据库的demo,发现原来是查询代码出了问题。

原代码如下:

public List<Map<String,Object>> queryAllJournal(){
        List<Map<String,Object>> journalData = new ArrayList<Map<String,Object>>();
        if(db != null){
            Cursor cursor = db.query(JournalTB.JOURNAL_TABLE, null, null, null, null, null, null);
            if (cursor.getCount() > 0){
                System.out.println("DDP: cursor.count = " + cursor.getCount());
                cursor.moveToFirst();
                HashMap item = null;
                while(!cursor.isAfterLast()){   // 仔细看看,这是一个死循环啊,cursor位置没有改变啊!!!
                    item = new HashMap<String,Object>();
                    item.put("id", cursor.getInt(JournalTB.ID_COL_INDEX));
                    item.put("date", cursor.getString(JournalTB.DATE_COL_INDEX));
                    item.put("description", cursor.getString(JournalTB.DESCRIPTION_COL_INDEX));
                    journalData.add(item);
                }

            }
            cursor.close();
        }
        
        return journalData;
    }


怎么解决呢?

仔细看了好久,唉,真是该死啊!  有一个死循环在 代码中。

忘记了下面这一句!,唉,悲剧啊

cursor.moveToNext();


你可能感兴趣的:(android,数据库,object,HashMap,null,Allocation)