简体   繁体   中英

Out of memory error when inserting more records(nearly 1,00,000 records) into database

我正在通过后台线程向数据库中插入1,00,000条记录。这一次,当我想加载Ui屏幕时,我遇到了内存不足错误。例如,当堆大小为5 MB且将4MB分配给后台线程时,则2 MB为加载UI屏幕是必需的。因此在这种情况下,我遇到了内存不足的问题。请告诉我在这里我需要做些什么来解决内存不足的问题。

I think you should use less memory...

Those 1,000,000 records come from somewhere. Don't keep them in memory all at once - if you read them, write them to the database as you read them - one chunk at a time (say, 1,000 at a time?). If you generate them, don't generate all of them at once, but rather - a chunk at a time.

Try inserting 100 at a time instead of 1,000 at a time. That should cut your background thread's memory usage by quite a bit.

If your records are coming from the server as XML, don't use a DOM parser; it is a memory hog. Use a SAX, StAX, or pull parser instead.

Finally, be sure that you aren't keeping references to objects that you don't need once the records are inserted.

Insert all your records from seperate thread different from main thread or process .Use IntentService class in android for inserting large number of entries in your database which does not effect your UI. for ex:

 public class BackgroundService extends IntentService {
            EventDataSQLHelper eventsData;
            SQLiteDatabase dbx ;
            SQLiteDatabase rdbx;
            ContentValues values;

            public BackgroundService() {
                super("BackgroundService");
            }

            @Override
            protected void onHandleIntent(Intent intent) {
                // TODO Auto-generated method stub
                eventsData=new EventDataSQLHelper(BackgroundService.this);
                values=new ContentValues();
                dbx = eventsData.getWritableDatabase();    
                rdbx = eventsData.getReadableDatabase();
                for(int i=0;i<100000;i++)
        {
        dbx.insert("tablename", null,values);

        }


                dbx.close();


            }


            }

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